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