blob: 9d4f4eccd9adb0f1c551438be8a25cf7a6807dd0 [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.
wahidw761934a2019-11-27 06:07:26 +000017
18
19 This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 platform project (RICP).
21
kalnagy92162652019-07-02 15:15:49 +020022==================================================================================
23*/
24/*
25 Mnemonic: httpgetter.go
26 Abstract: HTTPgetter unit tests
27 Date: 14 May 2019
28*/
29
30package nbi
31
32import (
33 "net"
34 "net/http"
35 "net/http/httptest"
kalnagy92162652019-07-02 15:15:49 +020036 "testing"
rangajalcb184222021-06-11 13:17:22 +000037 "routing-manager/pkg/sbi"
38 "routing-manager/pkg/sdl"
39 "routing-manager/pkg/rpe"
40 "fmt"
41 "sync"
kalnagy92162652019-07-02 15:15:49 +020042)
43
44var (
zkoczkaaaf8d392019-10-02 17:16:06 +020045 XMURL = "http://127.0.0.1:3000/ric/v1/xapps"
rangajalcb184222021-06-11 13:17:22 +000046 E2MURL = "http://127.0.0.1:8085/ric/v1/e2t/list"
kalnagy92162652019-07-02 15:15:49 +020047)
48
kalnagy92162652019-07-02 15:15:49 +020049func TestFetchXappListInvalidData(t *testing.T) {
50 var httpGetter = NewHttpGetter()
zkoczkaaaf8d392019-10-02 17:16:06 +020051 _, err := httpGetter.FetchAllXApps(XMURL)
kalnagy92162652019-07-02 15:15:49 +020052 if err == nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020053 t.Error("No XApp data received: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020054 }
55}
56
kalnagy92162652019-07-02 15:15:49 +020057func TestFetchXappListWithInvalidData(t *testing.T) {
zkoczkaaaf8d392019-10-02 17:16:06 +020058 var expected = 0
kalnagy92162652019-07-02 15:15:49 +020059 b := []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
60 l, err := net.Listen("tcp", "127.0.0.1:3000")
61 if err != nil {
62 t.Error("Failed to create listener: " + err.Error())
63 }
64 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65 //t.Log(r.Method)
66 //t.Log(r.URL)
67 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
68 //t.Log("Sending reply")
69 w.Header().Add("Content-Type", "application/json")
70 w.WriteHeader(http.StatusOK)
71 w.Write(b)
72 }
73 }))
74 ts.Listener.Close()
75 ts.Listener = l
76
77 ts.Start()
78 defer ts.Close()
79 var httpGetter = NewHttpGetter()
zkoczkaaaf8d392019-10-02 17:16:06 +020080 xapplist, err := httpGetter.FetchAllXApps(XMURL)
kalnagy92162652019-07-02 15:15:49 +020081 if err == nil {
82 t.Error("Error occured: " + err.Error())
83 } else {
84 //t.Log(len(*xapplist))
85 if len(*xapplist) != expected {
86 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
87 }
88 }
89}
90
zkoczkaaaf8d392019-10-02 17:16:06 +020091func TestFetchAllXAppsWithValidData(t *testing.T) {
92 var expected = 1
kalnagy92162652019-07-02 15:15:49 +020093 b := []byte(`[
94 {
95 "name":"xapp-01","status":"unknown","version":"1.2.3",
96 "instances":[
97 {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
98 "txMessages":["ControlIndication"],
99 "rxMessages":["LoadIndication","Reset"]
100 },
101 {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
102 "txMessages":["ControlIndication","SNStatusTransfer"],
103 "rxMessages":["LoadIndication","HandoverPreparation"]
104 }
105 ]
106}
107]`)
108 l, err := net.Listen("tcp", "127.0.0.1:3000")
109 if err != nil {
110 t.Error("Failed to create listener: " + err.Error())
111 }
112 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
113 //t.Log(r.Method)
114 //t.Log(r.URL)
115 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
116 //t.Log("Sending reply")
117 w.Header().Add("Content-Type", "application/json")
118 w.WriteHeader(http.StatusOK)
119 w.Write(b)
120 }
121 }))
122 ts.Listener.Close()
123 ts.Listener = l
124
125 ts.Start()
126 defer ts.Close()
127 var httpGetter = NewHttpGetter()
zkoczkaaaf8d392019-10-02 17:16:06 +0200128 xapplist, err := httpGetter.FetchAllXApps(XMURL)
kalnagy92162652019-07-02 15:15:49 +0200129 if err != nil {
130 t.Error("Error occured: " + err.Error())
131 } else {
132 if len(*xapplist) != expected {
133 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
134 }
135 }
136}
rangajalcb184222021-06-11 13:17:22 +0000137
138func TestHttpInstance1(t *testing.T) {
139 sdlEngine, _ := sdl.GetSdl("file")
140 rpeEngine, _ := rpe.GetRpe("rmrpush")
141 sbiEngine, _ := sbi.GetSbi("rmrpush")
142 httpinstance := NewHttpGetter()
143 err := httpinstance.Terminate()
144 t.Log(err)
145 fmt.Printf("sbiEngine = %v", sbiEngine)
146
147 createMockPlatformComponents()
148 //ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, nil)
149 //ts.Start()
150 //defer ts.Close()
151 var m sync.Mutex
152 err = httpinstance.Initialize(XMURL, "httpgetter", "rt.json", "config.json", E2MURL, sdlEngine, rpeEngine, &m)
153}
154