Added RSM platform component routes and xApp Manager interface changes
[ric-plt/rtmgr.git] / pkg / nbi / nbi_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 /*
20   Mnemonic:     nbi_test.go
21   Abstract:     NBI unit tests
22   Date:         21 May 2019
23 */
24
25 package nbi
26
27 import (
28         "errors"
29         "github.com/go-openapi/swag"
30         "net"
31         "net/http"
32         "net/http/httptest"
33         "reflect"
34         "routing-manager/pkg/appmgr_model"
35         "testing"
36 )
37
38 func TestGetNbi(t *testing.T) {
39         var errtype = errors.New("")
40         var nbitype = new(HttpGetter)
41         var invalids = []string{"httpgetter", ""}
42
43         nbii, err := GetNbi("httpGetter")
44         if err != nil {
45                 t.Errorf("GetNbi(HttpGetter) was incorrect, got: %v, want: %v.", reflect.TypeOf(err), nil)
46         }
47         if reflect.TypeOf(nbii) != reflect.TypeOf(nbitype) {
48                 t.Errorf("GetNbi(HttpGetter) was incorrect, got: %v, want: %v.", reflect.TypeOf(nbii), reflect.TypeOf(nbitype))
49         }
50
51         for _, arg := range invalids {
52                 _, err := GetNbi(arg)
53                 if err == nil {
54                         t.Errorf("GetNbi("+arg+") was incorrect, got: %v, want: %v.", reflect.TypeOf(err), reflect.TypeOf(errtype))
55                 }
56         }
57 }
58 func TestCreateSubReq(t *testing.T) {
59         var subData = appmgr_model.SubscriptionData{
60                 TargetURL:  swag.String("localhost:8000/ric/v1/handles/xapp-handle/"),
61                 EventType:  appmgr_model.EventTypeAll,
62                 MaxRetries: swag.Int64(5),
63                 RetryTimer: swag.Int64(10),
64         }
65         subReq := appmgr_model.SubscriptionRequest{
66                 Data: &subData,
67         }
68         subReq2 := CreateSubReq("localhost", "8000")
69         if reflect.TypeOf(subReq) != reflect.TypeOf(*subReq2) {
70                 t.Errorf("Invalid type, got: %v, want: %v.", reflect.TypeOf(subReq), reflect.TypeOf(*subReq2))
71         }
72         if *(subReq.Data.TargetURL) != *(subReq2.Data.TargetURL) {
73                 t.Errorf("Invalid TargetURL generated, got %v, want %v", *subReq.Data.TargetURL, *subReq2.Data.TargetURL)
74         }
75
76         if (subReq.Data.EventType) != (subReq2.Data.EventType) {
77                 t.Errorf("Invalid EventType generated, got %v, want %v", subReq.Data.EventType, subReq2.Data.EventType)
78         }
79
80         if *(subReq.Data.MaxRetries) != *(subReq2.Data.MaxRetries) {
81                 t.Errorf("Invalid MaxRetries generated, got %v, want %v", *subReq.Data.MaxRetries, *subReq2.Data.MaxRetries)
82         }
83         if *(subReq.Data.RetryTimer) != *(subReq2.Data.RetryTimer) {
84                 t.Errorf("Invalid RetryTimer generated, got %v, want %v", *subReq.Data.RetryTimer, *subReq2.Data.RetryTimer)
85         }
86
87 }
88
89 func TestPostSubReq(t *testing.T) {
90         b := []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
91         l, err := net.Listen("tcp", "127.0.0.1:3000")
92         if err != nil {
93                 t.Error("Failed to create listener: " + err.Error())
94         }
95         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
96                 t.Log(r.Method)
97                 t.Log(r.URL)
98                 if r.Method == "POST" && r.URL.String() == "/ric/v1/subscriptions" {
99                         t.Log("Sending reply")
100                         w.Header().Add("Content-Type", "application/json")
101                         w.WriteHeader(http.StatusCreated)
102                         w.Write(b)
103                 }
104         }))
105         ts.Listener.Close()
106         ts.Listener = l
107
108         ts.Start()
109         defer ts.Close()
110         err = PostSubReq("http://127.0.0.1:3000/ric/v1/subscription", "localhost:8888")
111         if err != nil {
112                 t.Error("Error occured: " + err.Error())
113         }
114 }
115
116 func TestPostSubReqWithInvalidUrls(t *testing.T) {
117         // invalid Xapp Manager URL
118         err := PostSubReq("http://127.0", "http://localhost:8888")
119         if err == nil {
120                 t.Error("Error occured: " + err.Error())
121         }
122         // invalid rest api url
123         err = PostSubReq("http://127.0.0.1:3000/", "localhost:8888")
124         if err == nil {
125                 t.Error("Error occured: " + err.Error())
126         }
127 }