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
28 "routing-manager/pkg/stub"
29 "routing-manager/pkg/models"
30 "routing-manager/pkg/sdl"
31 "github.com/go-openapi/swag"
43 var BASIC_XAPPLIST = []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 SUBSCRIPTION_RESP = []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
62 var INVALID_SUB_RESP = []byte(`{"Version":0, "EventType":all}`)
65 func createMockAppmgrWithData(url string, g []byte, p []byte) *httptest.Server {
66 l, err := net.Listen("tcp", url)
68 fmt.Println("Failed to create listener: " + err.Error())
70 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
71 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
72 w.Header().Add("Content-Type", "application/json")
73 w.WriteHeader(http.StatusOK)
76 if r.Method == "POST" && r.URL.String() == "/ric/v1/subscriptions" {
77 w.Header().Add("Content-Type", "application/json")
78 w.WriteHeader(http.StatusCreated)
88 func createMockPlatformComponents() {
89 var filename = string("config.json")
90 file, _ := json.MarshalIndent(stub.ValidPlatformComponents, "", "")
91 filestr := string(file)
92 filestr = "{\"PlatformComponents\":"+filestr+"}"
93 file = []byte(filestr)
94 _ = ioutil.WriteFile(filename, file, 644)
97 func TestRecvXappCallbackData(t *testing.T) {
98 data := models.XappCallbackData {
99 XApps: *swag.String("[]"),
100 Version: *swag.Int64(1),
101 Event: *swag.String("any"),
102 ID: *swag.String("123456"),
105 ch := make(chan *models.XappCallbackData)
107 httpRestful := NewHttpRestful()
108 go func() {ch<- &data}()
109 time.Sleep(1 * time.Second)
110 t.Log(string(len(ch)))
111 xappList, err := httpRestful.RecvXappCallbackData(ch)
113 t.Error("Receive failed: "+err.Error())
116 t.Error("Expected an XApp notification list")
123 func TestProvideXappHandleHandlerImpl(t *testing.T) {
124 datach := make(chan *models.XappCallbackData, 10)
126 data := models.XappCallbackData{
127 XApps: *swag.String("[]"),
128 Version: *swag.Int64(1),
129 Event: *swag.String("someevent"),
130 ID: *swag.String("123456")}
131 var httpRestful, _ = GetNbi("httpRESTful")
132 err := httpRestful.(*HttpRestful).ProvideXappHandleHandlerImpl(datach, &data)
134 t.Error("Error occured: "+err.Error())
138 t.Error("Something gone wrong: "+err.Error())
141 t.Error("Malformed data on channel")
147 func TestValidateXappCallbackData(t *testing.T) {
148 data := models.XappCallbackData{
149 XApps: *swag.String("[]"),
150 Version: *swag.Int64(1),
151 Event: *swag.String("someevent"),
152 ID: *swag.String("123456")}
154 err := validateXappCallbackData(&data)
156 t.Error("Invalid XApp callback data: "+err.Error())
160 func TestValidateXappCallbackDataWithInvalidData(t *testing.T) {
161 data := models.XappCallbackData{
162 XApps: *swag.String("{}"),
163 Version: *swag.Int64(1),
164 Event: *swag.String("someevent"),
165 ID: *swag.String("123456")}
167 err := validateXappCallbackData(&data)
169 t.Error("Invalid XApp callback data: "+err.Error())
174 func TestHttpGetXappsInvalidData(t *testing.T) {
175 _, err := httpGetXapps(XMURL)
177 t.Error("No XApp data received: "+err.Error())
181 func TestHttpGetXappsWithValidData(t *testing.T) {
183 ts := createMockAppmgrWithData("127.0.0.1:3000", BASIC_XAPPLIST, nil)
187 xapplist, err := httpGetXapps(XMURL)
189 t.Error("Error occured: " + err.Error())
191 if len(*xapplist) != expected {
192 t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
197 func TestRetrieveStartupDataTimeout(t *testing.T) {
198 sdlEngine, _ := sdl.GetSdl("file")
199 createMockPlatformComponents()
200 err := retrieveStartupData(XMURL, "httpgetter","rt.json", "config.json", sdlEngine)
202 t.Error("Cannot retrieve startup data: "+err.Error())
205 os.Remove("config.json")
208 func TestRetrieveStartupData(t *testing.T) {
209 ts := createMockAppmgrWithData("127.0.0.1:3000", BASIC_XAPPLIST, SUBSCRIPTION_RESP)
212 sdlEngine, _ := sdl.GetSdl("file")
213 var httpRestful, _ = GetNbi("httpRESTful")
214 createMockPlatformComponents()
215 err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json","config.json", sdlEngine)
216 //err := retrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
218 t.Error("Cannot retrieve startup data: "+err.Error())
221 os.Remove("config.json")
224 func TestRetrieveStartupDataWithInvalidSubResp(t *testing.T) {
225 ts := createMockAppmgrWithData("127.0.0.1:3000", BASIC_XAPPLIST, INVALID_SUB_RESP)
228 sdlEngine, _ := sdl.GetSdl("file")
229 var httpRestful, _ = GetNbi("httpRESTful")
230 createMockPlatformComponents()
231 err := httpRestful.(*HttpRestful).RetrieveStartupData(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine)
233 t.Error("Cannot retrieve startup data: "+err.Error())
236 os.Remove("config.json")