RIC:1060: Change in PTL
[ric-plt/rtmgr.git] / pkg / nbi / httpgetter_test.go
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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22 ==================================================================================
23 */
24 /*
25   Mnemonic:     httpgetter.go
26   Abstract:     HTTPgetter unit tests
27   Date:         14 May 2019
28 */
29
30 package nbi
31
32 import (
33         "net"
34         "net/http"
35         "net/http/httptest"
36         "testing"
37         "routing-manager/pkg/sbi"
38         "routing-manager/pkg/sdl"
39         "routing-manager/pkg/rpe"
40         "fmt"
41         "sync"
42 )
43
44 var (
45         XMURL = "http://127.0.0.1:3000/ric/v1/xapps"
46         E2MURL = "http://127.0.0.1:8085/ric/v1/e2t/list"
47 )
48
49 func TestFetchXappListInvalidData(t *testing.T) {
50         var httpGetter = NewHttpGetter()
51         _, err := httpGetter.FetchAllXApps(XMURL)
52         if err == nil {
53                 t.Error("No XApp data received: " + err.Error())
54         }
55 }
56
57 func TestFetchXappListWithInvalidData(t *testing.T) {
58         var expected = 0
59         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()
80         xapplist, err := httpGetter.FetchAllXApps(XMURL)
81         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
91 func TestFetchAllXAppsWithValidData(t *testing.T) {
92         var expected = 1
93         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()
128         xapplist, err := httpGetter.FetchAllXApps(XMURL)
129         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 }
137
138 func 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