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