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.
17 ==================================================================================
20 Mnemonic: httprestful_test.go
21 Abstract: HTTPRestful unit tests
30 "github.com/go-openapi/swag"
36 "routing-manager/pkg/models"
37 "routing-manager/pkg/sdl"
38 "routing-manager/pkg/stub"
43 var BasicXAppLists = []byte(`[
45 "name":"xapp-01","status":"unknown","version":"1.2.3",
47 {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
48 "txMessages":["ControlIndication"],
49 "rxMessages":["LoadIndication","Reset"]
51 {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
52 "txMessages":["ControlIndication","SNStatusTransfer"],
53 "rxMessages":["LoadIndication","HandoverPreparation"]
59 var SubscriptionResp = []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
61 var InvalidSubResp = []byte(`{"Version":0, "EventType":all}`)
63 func createMockAppmgrWithData(url string, g []byte, p []byte) *httptest.Server {
64 l, err := net.Listen("tcp", url)
66 fmt.Println("Failed to create listener: " + err.Error())
68 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
70 w.Header().Add("Content-Type", "application/json")
71 w.WriteHeader(http.StatusOK)
74 if r.Method == "POST" && r.URL.String() == "/ric/v1/subscriptions" {
75 w.Header().Add("Content-Type", "application/json")
76 w.WriteHeader(http.StatusCreated)
86 func createMockPlatformComponents() {
87 var filename = "config.json"
88 file, _ := json.MarshalIndent(stub.ValidPlatformComponents, "", "")
89 filestr := string(file)
90 filestr = "{\"PlatformComponents\":" + filestr + "}"
91 file = []byte(filestr)
92 _ = ioutil.WriteFile(filename, file, 644)
95 func TestRecvXappCallbackData(t *testing.T) {
96 data := models.XappCallbackData{
97 XApps: *swag.String("[]"),
98 Version: *swag.Int64(1),
99 Event: *swag.String("any"),
100 ID: *swag.String("123456"),
103 ch := make(chan *models.XappCallbackData)
105 httpRestful := NewHttpRestful()
106 go func() { ch <- &data }()
107 time.Sleep(1 * time.Second)
108 t.Log(string(len(ch)))
109 xappList, err := httpRestful.RecvXappCallbackData(ch)
111 t.Error("Receive failed: " + err.Error())
114 t.Error("Expected an XApp notification list")
121 func TestProvideXappHandleHandlerImpl(t *testing.T) {
122 datach := make(chan *models.XappCallbackData, 10)
124 data := models.XappCallbackData{
125 XApps: *swag.String("[]"),
126 Version: *swag.Int64(1),
127 Event: *swag.String("someevent"),
128 ID: *swag.String("123456")}
129 var httpRestful, _ = GetNbi("httpRESTful")
130 err := httpRestful.(*HttpRestful).ProvideXappHandleHandlerImpl(datach, &data)
132 t.Error("Error occured: " + err.Error())
136 t.Error("Something gone wrong: " + err.Error())
139 t.Error("Malformed data on channel")
145 func TestValidateXappCallbackData(t *testing.T) {
146 data := models.XappCallbackData{
147 XApps: *swag.String("[]"),
148 Version: *swag.Int64(1),
149 Event: *swag.String("someevent"),
150 ID: *swag.String("123456")}
152 err := validateXappCallbackData(&data)
154 t.Error("Invalid XApp callback data: " + err.Error())
158 func TestValidateXappCallbackDataWithInvalidData(t *testing.T) {
159 data := models.XappCallbackData{
160 XApps: *swag.String("{}"),
161 Version: *swag.Int64(1),
162 Event: *swag.String("someevent"),
163 ID: *swag.String("123456")}
165 err := validateXappCallbackData(&data)
167 t.Error("Invalid XApp callback data: " + err.Error())
171 func TestHttpGetXAppsInvalidData(t *testing.T) {
172 _, err := httpGetXApps(XMURL)
174 t.Error("No XApp data received: " + err.Error())
178 func TestHttpGetXAppsWithValidData(t *testing.T) {
180 ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, nil)
184 xapplist, err := httpGetXApps(XMURL)
186 t.Error("Error occured: " + err.Error())
188 if len(*xapplist) != expected {
189 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
194 func TestRetrieveStartupDataTimeout(t *testing.T) {
195 sdlEngine, _ := sdl.GetSdl("file")
196 createMockPlatformComponents()
197 err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
199 t.Error("Cannot retrieve startup data: " + err.Error())
202 os.Remove("config.json")
205 func TestRetrieveStartupData(t *testing.T) {
206 ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, SubscriptionResp)
209 sdlEngine, _ := sdl.GetSdl("file")
210 var httpRestful, _ = GetNbi("httpRESTful")
211 createMockPlatformComponents()
212 err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
213 //err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
215 t.Error("Cannot retrieve startup data: " + err.Error())
218 os.Remove("config.json")
221 func TestRetrieveStartupDataWithInvalidSubResp(t *testing.T) {
222 ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, InvalidSubResp)
225 sdlEngine, _ := sdl.GetSdl("file")
226 var httpRestful, _ = GetNbi("httpRESTful")
227 createMockPlatformComponents()
228 err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
230 t.Error("Cannot retrieve startup data: " + err.Error())
233 os.Remove("config.json")