0dc65a29908f9aa7b5fe18ed6f58ae2a75bc05a7
[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         "routing-manager/pkg/rtmgr"
37         "testing"
38 )
39
40 var (
41         XMURL = "http://127.0.0.1:3000/ric/v1/xapps"
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         rtmgr.SetLogLevel("debug")
55         b := []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
56         l, err := net.Listen("tcp", "127.0.0.1:3000")
57         if err != nil {
58                 t.Error("Failed to create listener: " + err.Error())
59         }
60         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61                 //t.Log(r.Method)
62                 //t.Log(r.URL)
63                 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
64                         //t.Log("Sending reply")
65                         w.Header().Add("Content-Type", "application/json")
66                         w.WriteHeader(http.StatusOK)
67                         w.Write(b)
68                 }
69         }))
70         ts.Listener.Close()
71         ts.Listener = l
72
73         ts.Start()
74         defer ts.Close()
75         var httpGetter = NewHttpGetter()
76         xapplist, err := httpGetter.FetchAllXApps(XMURL)
77         if err == nil {
78                 t.Error("Error occured: " + err.Error())
79         } else {
80                 //t.Log(len(*xapplist))
81                 if len(*xapplist) != expected {
82                         t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
83                 }
84         }
85 }
86
87 func TestFetchAllXAppsWithValidData(t *testing.T) {
88         var expected = 1
89         b := []byte(`[
90  {
91  "name":"xapp-01","status":"unknown","version":"1.2.3",
92     "instances":[
93         {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
94             "txMessages":["ControlIndication"],
95             "rxMessages":["LoadIndication","Reset"]
96         },
97         {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
98             "txMessages":["ControlIndication","SNStatusTransfer"],
99             "rxMessages":["LoadIndication","HandoverPreparation"]
100         }
101     ]
102 }
103 ]`)
104         l, err := net.Listen("tcp", "127.0.0.1:3000")
105         if err != nil {
106                 t.Error("Failed to create listener: " + err.Error())
107         }
108         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
109                 //t.Log(r.Method)
110                 //t.Log(r.URL)
111                 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
112                         //t.Log("Sending reply")
113                         w.Header().Add("Content-Type", "application/json")
114                         w.WriteHeader(http.StatusOK)
115                         w.Write(b)
116                 }
117         }))
118         ts.Listener.Close()
119         ts.Listener = l
120
121         ts.Start()
122         defer ts.Close()
123         var httpGetter = NewHttpGetter()
124         xapplist, err := httpGetter.FetchAllXApps(XMURL)
125         if err != nil {
126                 t.Error("Error occured: " + err.Error())
127         } else {
128                 if len(*xapplist) != expected {
129                         t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
130                 }
131         }
132 }