blob: 5c7082a6f23c6eb26be98934506de3ef80ef37c4 [file] [log] [blame]
kalnagy92162652019-07-02 15:15:49 +02001/*
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
25package nbi
26
27import (
28 "net"
29 "net/http"
30 "net/http/httptest"
31 "routing-manager/pkg/rtmgr"
32 "testing"
33)
34
35var (
zkoczkaaaf8d392019-10-02 17:16:06 +020036 XMURL = "http://127.0.0.1:3000/ric/v1/xapps"
kalnagy92162652019-07-02 15:15:49 +020037)
38
kalnagy92162652019-07-02 15:15:49 +020039func TestFetchXappListInvalidData(t *testing.T) {
40 var httpGetter = NewHttpGetter()
zkoczkaaaf8d392019-10-02 17:16:06 +020041 _, err := httpGetter.FetchAllXApps(XMURL)
kalnagy92162652019-07-02 15:15:49 +020042 if err == nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020043 t.Error("No XApp data received: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020044 }
45}
46
kalnagy92162652019-07-02 15:15:49 +020047func TestFetchXappListWithInvalidData(t *testing.T) {
zkoczkaaaf8d392019-10-02 17:16:06 +020048 var expected = 0
kalnagy92162652019-07-02 15:15:49 +020049 rtmgr.SetLogLevel("debug")
50 b := []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
51 l, err := net.Listen("tcp", "127.0.0.1:3000")
52 if err != nil {
53 t.Error("Failed to create listener: " + err.Error())
54 }
55 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56 //t.Log(r.Method)
57 //t.Log(r.URL)
58 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
59 //t.Log("Sending reply")
60 w.Header().Add("Content-Type", "application/json")
61 w.WriteHeader(http.StatusOK)
62 w.Write(b)
63 }
64 }))
65 ts.Listener.Close()
66 ts.Listener = l
67
68 ts.Start()
69 defer ts.Close()
70 var httpGetter = NewHttpGetter()
zkoczkaaaf8d392019-10-02 17:16:06 +020071 xapplist, err := httpGetter.FetchAllXApps(XMURL)
kalnagy92162652019-07-02 15:15:49 +020072 if err == nil {
73 t.Error("Error occured: " + err.Error())
74 } else {
75 //t.Log(len(*xapplist))
76 if len(*xapplist) != expected {
77 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
78 }
79 }
80}
81
zkoczkaaaf8d392019-10-02 17:16:06 +020082func TestFetchAllXAppsWithValidData(t *testing.T) {
83 var expected = 1
kalnagy92162652019-07-02 15:15:49 +020084 b := []byte(`[
85 {
86 "name":"xapp-01","status":"unknown","version":"1.2.3",
87 "instances":[
88 {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
89 "txMessages":["ControlIndication"],
90 "rxMessages":["LoadIndication","Reset"]
91 },
92 {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
93 "txMessages":["ControlIndication","SNStatusTransfer"],
94 "rxMessages":["LoadIndication","HandoverPreparation"]
95 }
96 ]
97}
98]`)
99 l, err := net.Listen("tcp", "127.0.0.1:3000")
100 if err != nil {
101 t.Error("Failed to create listener: " + err.Error())
102 }
103 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
104 //t.Log(r.Method)
105 //t.Log(r.URL)
106 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
107 //t.Log("Sending reply")
108 w.Header().Add("Content-Type", "application/json")
109 w.WriteHeader(http.StatusOK)
110 w.Write(b)
111 }
112 }))
113 ts.Listener.Close()
114 ts.Listener = l
115
116 ts.Start()
117 defer ts.Close()
118 var httpGetter = NewHttpGetter()
zkoczkaaaf8d392019-10-02 17:16:06 +0200119 xapplist, err := httpGetter.FetchAllXApps(XMURL)
kalnagy92162652019-07-02 15:15:49 +0200120 if err != nil {
121 t.Error("Error occured: " + err.Error())
122 } else {
123 if len(*xapplist) != expected {
124 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
125 }
126 }
127}