2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
19 This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 platform project (RICP).
22 ==================================================================================
25 Mnemonic: httprestful_test.go
26 Abstract: HTTPRestful unit tests
40 "routing-manager/pkg/models"
41 "routing-manager/pkg/sdl"
42 "routing-manager/pkg/stub"
46 "github.com/go-openapi/swag"
49 var BasicXAppLists = []byte(`[
51 "name":"xapp-01","status":"unknown","version":"1.2.3",
53 {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
54 "txMessages":["ControlIndication"],
55 "rxMessages":["LoadIndication","Reset"]
57 {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
58 "txMessages":["ControlIndication","SNStatusTransfer"],
59 "rxMessages":["LoadIndication","HandoverPreparation"]
65 var SubscriptionResp = []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
67 var InvalidSubResp = []byte(`{"Version":0, "EventType":all}`)
69 func createMockAppmgrWithData(url string, g []byte, p []byte) *httptest.Server {
70 l, err := net.Listen("tcp", url)
72 fmt.Println("Failed to create listener: " + err.Error())
74 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
76 w.Header().Add("Content-Type", "application/json")
77 w.WriteHeader(http.StatusOK)
80 if r.Method == "POST" && r.URL.String() == "/ric/v1/subscriptions" {
81 w.Header().Add("Content-Type", "application/json")
82 w.WriteHeader(http.StatusCreated)
92 func createMockPlatformComponents() {
93 var filename = "config.json"
94 file, _ := json.MarshalIndent(stub.ValidPlatformComponents, "", "")
95 filestr := string(file)
96 filestr = "{\"PlatformComponents\":" + filestr + "}"
97 file = []byte(filestr)
98 _ = ioutil.WriteFile(filename, file, 644)
101 func TestRecvXappCallbackData(t *testing.T) {
102 data := models.XappCallbackData{
103 XApps: *swag.String("[]"),
104 Version: *swag.Int64(1),
105 Event: *swag.String("any"),
106 ID: *swag.String("123456"),
109 ch := make(chan *models.XappCallbackData)
111 httpRestful := NewHttpRestful()
112 go func() { ch <- &data }()
113 time.Sleep(1 * time.Second)
114 t.Log(string(len(ch)))
115 xappList, err := httpRestful.RecvXappCallbackData(ch)
117 t.Error("Receive failed: " + err.Error())
120 t.Error("Expected an XApp notification list")
127 func TestProvideXappHandleHandlerImpl(t *testing.T) {
128 datach := make(chan *models.XappCallbackData, 10)
130 data := models.XappCallbackData{
131 XApps: *swag.String("[]"),
132 Version: *swag.Int64(1),
133 Event: *swag.String("someevent"),
134 ID: *swag.String("123456")}
135 var httpRestful, _ = GetNbi("httpRESTful")
136 err := httpRestful.(*HttpRestful).ProvideXappHandleHandlerImpl(datach, &data)
138 t.Error("Error occured: " + err.Error())
142 t.Error("Something gone wrong: " + err.Error())
145 t.Error("Malformed data on channel")
151 func TestValidateXappCallbackData(t *testing.T) {
152 data := models.XappCallbackData{
153 XApps: *swag.String("[]"),
154 Version: *swag.Int64(1),
155 Event: *swag.String("someevent"),
156 ID: *swag.String("123456")}
158 err := validateXappCallbackData(&data)
160 t.Error("Invalid XApp callback data: " + err.Error())
164 func TestValidateXappCallbackDataWithInvalidData(t *testing.T) {
165 data := models.XappCallbackData{
166 XApps: *swag.String("{}"),
167 Version: *swag.Int64(1),
168 Event: *swag.String("someevent"),
169 ID: *swag.String("123456")}
171 err := validateXappCallbackData(&data)
173 t.Error("Invalid XApp callback data: " + err.Error())
177 func TestHttpGetXAppsInvalidData(t *testing.T) {
178 _, err := httpGetXApps(XMURL)
180 t.Error("No XApp data received: " + err.Error())
184 func TestHttpGetXAppsWithValidData(t *testing.T) {
186 ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, nil)
190 xapplist, err := httpGetXApps(XMURL)
192 t.Error("Error occured: " + err.Error())
194 if len(*xapplist) != expected {
195 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
200 func TestRetrieveStartupDataTimeout(t *testing.T) {
201 sdlEngine, _ := sdl.GetSdl("file")
202 createMockPlatformComponents()
203 err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
205 t.Error("Cannot retrieve startup data: " + err.Error())
208 os.Remove("config.json")
211 func TestRetrieveStartupData(t *testing.T) {
212 ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, SubscriptionResp)
215 sdlEngine, _ := sdl.GetSdl("file")
216 var httpRestful, _ = GetNbi("httpRESTful")
217 createMockPlatformComponents()
218 err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
219 //err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
221 t.Error("Cannot retrieve startup data: " + err.Error())
224 os.Remove("config.json")
227 func TestRetrieveStartupDataWithInvalidSubResp(t *testing.T) {
228 ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, InvalidSubResp)
231 sdlEngine, _ := sdl.GetSdl("file")
232 var httpRestful, _ = GetNbi("httpRESTful")
233 createMockPlatformComponents()
234 err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
236 t.Error("Cannot retrieve startup data: " + err.Error())
239 os.Remove("config.json")