kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 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 ( |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 36 | XMURL = "http://127.0.0.1:3000/ric/v1/xapps" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 37 | ) |
| 38 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 39 | func TestFetchXappListInvalidData(t *testing.T) { |
| 40 | var httpGetter = NewHttpGetter() |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 41 | _, err := httpGetter.FetchAllXApps(XMURL) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 42 | if err == nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 43 | t.Error("No XApp data received: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 47 | func TestFetchXappListWithInvalidData(t *testing.T) { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 48 | var expected = 0 |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 49 | 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() |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 71 | xapplist, err := httpGetter.FetchAllXApps(XMURL) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 72 | 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 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 82 | func TestFetchAllXAppsWithValidData(t *testing.T) { |
| 83 | var expected = 1 |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 84 | 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() |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 119 | xapplist, err := httpGetter.FetchAllXApps(XMURL) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 120 | 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 | } |