Adding Unit Test cases for rtmgr

Change-Id: Id002c79cc1fb9562865b895eea6b5b9e84037d62
Signed-off-by: wahidw <abdulwahid.w@nokia.com>
diff --git a/pkg/nbi/httprestful_test.go b/pkg/nbi/httprestful_test.go
index b9d88c6..cfa644e 100644
--- a/pkg/nbi/httprestful_test.go
+++ b/pkg/nbi/httprestful_test.go
@@ -38,6 +38,8 @@
 	"net/http/httptest"
 	"os"
 	"routing-manager/pkg/models"
+	"routing-manager/pkg/rpe"
+	"routing-manager/pkg/rtmgr"
 	"routing-manager/pkg/sdl"
 	"routing-manager/pkg/stub"
 	"testing"
@@ -66,6 +68,172 @@
 
 var InvalidSubResp = []byte(`{"Version":0, "EventType":all}`)
 
+func TestValidateXappCallbackData_1(t *testing.T) {
+	data := models.XappCallbackData{
+		XApps:   *swag.String("[]"),
+		Version: *swag.Int64(1),
+		Event:   *swag.String("someevent"),
+		ID:      *swag.String("123456")}
+
+	err := validateXappCallbackData(&data)
+	if err != nil {
+		t.Error("Invalid XApp callback data: " + err.Error())
+	}
+}
+
+func TestValidateXappSubscriptionsData(t *testing.T) {
+
+	ep := make(map[string]*rtmgr.Endpoint)
+	ep["dummy"] = &rtmgr.Endpoint{Uuid: "10.0.0.1:0", Name: "E2TERM", XAppType: "app1", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: true}
+	p := uint16(1234)
+	data := models.XappSubscriptionData{
+		Address:        swag.String("10.1.1.1"),
+		Port:           &p,
+		SubscriptionID: swag.Int32(123456)}
+
+	var err error
+	err = validateXappSubscriptionData(&data)
+	t.Log(err)
+
+	rtmgr.Eps = ep
+	p = uint16(0)
+	data1 := models.XappSubscriptionData{
+		Address:        swag.String(""),
+		Port:           &p,
+		SubscriptionID: swag.Int32(123456)}
+	err = validateXappSubscriptionData(&data1)
+	t.Log(err)
+
+	//Validate E2tData
+	data2 := models.E2tData{
+		E2TAddress: swag.String(""),
+	}
+	err = validateE2tData(&data2)
+
+	e2tchannel := make(chan *models.E2tData, 10)
+	_ = createNewE2tHandleHandlerImpl(e2tchannel, &data2)
+	defer close(e2tchannel)
+
+	//test case for provideXappSubscriptionHandleImp
+	datachannel := make(chan *models.XappSubscriptionData, 10)
+	_ = provideXappSubscriptionHandleImpl(datachannel, &data1)
+	defer close(datachannel)
+
+	//test case for deleteXappSubscriptionHandleImpl
+	_ = deleteXappSubscriptionHandleImpl(datachannel, &data1)
+}
+
+func TestvalidateE2tData(t *testing.T) {
+	data := models.E2tData{
+		E2TAddress: swag.String(""),
+	}
+	err := validateE2tData(&data)
+	t.Log(err)
+}
+
+func TestSubscriptionExists(t *testing.T) {
+	p := uint16(0)
+	data := models.XappSubscriptionData{
+		Address:        swag.String("10.0.0.0"),
+		Port:           &p,
+		SubscriptionID: swag.Int32(1234)}
+
+	rtmgr.Subs = *stub.ValidSubscriptions
+
+	yes_no := subscriptionExists(&data)
+	yes_no = addSubscription(&rtmgr.Subs, &data)
+	yes_no = addSubscription(&rtmgr.Subs, &data)
+	yes_no = delSubscription(&rtmgr.Subs, &data)
+	yes_no = delSubscription(&rtmgr.Subs, &data)
+	t.Log(yes_no)
+}
+
+func TestaddSubscriptions(t *testing.T) {
+	p := uint16(1)
+	subdata := models.XappSubscriptionData{
+		Address:        swag.String("10.0.0.0"),
+		Port:           &p,
+		SubscriptionID: swag.Int32(1234)}
+
+	rtmgr.Subs = *stub.ValidSubscriptions
+	yes_no := addSubscription(&rtmgr.Subs, &subdata)
+	t.Log(yes_no)
+}
+
+func TestHttpInstance(t *testing.T) {
+	sdlEngine, _ := sdl.GetSdl("file")
+	rpeEngine, _ := rpe.GetRpe("rmrpush")
+	httpinstance := NewHttpRestful()
+	err := httpinstance.Terminate()
+	t.Log(err)
+
+	triggerSBI := make(chan bool)
+	createMockPlatformComponents()
+	//ts := createMockAppmgrWithData("127.0.0.1:3000", BasicXAppLists, nil)
+	//ts.Start()
+	//defer ts.Close()
+	err = httpinstance.Initialize(XMURL, "httpgetter", "rt.json", "config.json", sdlEngine, rpeEngine, triggerSBI)
+}
+
+func TestXappCallbackDataChannelwithdata(t *testing.T) {
+	data := models.XappCallbackData{
+		XApps:   *swag.String("[]"),
+		Version: *swag.Int64(1),
+		Event:   *swag.String("someevent"),
+		ID:      *swag.String("123456")}
+	datach := make(chan *models.XappCallbackData, 1)
+	go func() { _, _ = recvXappCallbackData(datach) }()
+	defer close(datach)
+	datach <- &data
+}
+func TestXappCallbackDataChannelNodata(t *testing.T) {
+	datach := make(chan *models.XappCallbackData, 1)
+	go func() { _, _ = recvXappCallbackData(datach) }()
+	defer close(datach)
+}
+
+func TestE2TChannelwithData(t *testing.T) {
+	data2 := models.E2tData{
+		E2TAddress: swag.String(""),
+	}
+	dataChannel := make(chan *models.E2tData, 10)
+	go func() { _, _ = recvNewE2Tdata(dataChannel) }()
+	defer close(dataChannel)
+	dataChannel <- &data2
+}
+
+func TestE2TChannelwithNoData(t *testing.T) {
+	dataChannel := make(chan *models.E2tData, 10)
+	go func() { _, _ = recvNewE2Tdata(dataChannel) }()
+	defer close(dataChannel)
+}
+
+func TestprovideXappSubscriptionHandleImpl(t *testing.T) {
+	p := uint16(0)
+	data := models.XappSubscriptionData{
+		Address:        swag.String("10.0.0.0"),
+		Port:           &p,
+		SubscriptionID: swag.Int32(1234)}
+	datachannel := make(chan *models.XappSubscriptionData, 10)
+	go func() { _ = provideXappSubscriptionHandleImpl(datachannel, &data) }()
+	defer close(datachannel)
+	datachannel <- &data
+
+	//subdel test
+}
+
+func TestdeleteXappSubscriptionHandleImpl(t *testing.T) {
+	p := uint16(1)
+	subdeldata := models.XappSubscriptionData{
+		Address:        swag.String("10.0.0.0"),
+		Port:           &p,
+		SubscriptionID: swag.Int32(1234)}
+	subdelchannel := make(chan *models.XappSubscriptionData, 10)
+	go func() { _ = deleteXappSubscriptionHandleImpl(subdelchannel, &subdeldata) }()
+	defer close(subdelchannel)
+	subdelchannel <- &subdeldata
+}
+
 func createMockAppmgrWithData(url string, g []byte, p []byte) *httptest.Server {
 	l, err := net.Listen("tcp", url)
 	if err != nil {
@@ -196,7 +364,6 @@
 		}
 	}
 }
-
 func TestRetrieveStartupDataTimeout(t *testing.T) {
 	sdlEngine, _ := sdl.GetSdl("file")
 	createMockPlatformComponents()
@@ -238,3 +405,20 @@
 	os.Remove("rt.json")
 	os.Remove("config.json")
 }
+
+func TestrecvXappCallbackData(t *testing.T) {
+	data := models.E2tData{
+		E2TAddress: swag.String("123456")}
+
+	var err error
+
+	e2tch := make(chan *models.E2tData)
+	go func() { e2tch <- &data }()
+	time.Sleep(1 * time.Second)
+	t.Log(string(len(e2tch)))
+	defer close(e2tch)
+
+	var httpRestful, _ = GetNbi("httpRESTful")
+	_, err = httpRestful.(*HttpRestful).RecvNewE2Tdata(e2tch)
+	t.Log(err)
+}
diff --git a/pkg/rpe/rmr_test.go b/pkg/rpe/rmr_test.go
index bd42d0b..4538d97 100644
--- a/pkg/rpe/rmr_test.go
+++ b/pkg/rpe/rmr_test.go
@@ -48,7 +48,28 @@
 func TestRmrPushGeneratePolicies(t *testing.T) {
 	var rmrpush = RmrPush{}
 	resetTestDataset(stub.ValidEndpoints)
+        stub.ValidPlatformComponents = nil
+        rtmgr.Subs =  *stub.ValidSubscriptions
 
 	rawrt := rmrpush.GeneratePolicies(rtmgr.Eps)
 	t.Log(rawrt)
 }
+
+/*
+getEndpointByUuid: Pass empty and valid values
+*/
+func TestRmrgetEndpointByUuid(t *testing.T) {
+        var ep *rtmgr.Endpoint
+        ep = getEndpointByUuid("")
+        t.Logf("getEndpointByUuid() return was correct, got: %v, want: %v.", ep, "<nil>")
+        ep = getEndpointByUuid("10.0.0.1:0")
+}
+
+/*
+GetRpe Instance with empty and valid values
+*/
+func TestRmrGetRpe(t *testing.T) {
+        _,_ = GetRpe("")
+        _,_ = GetRpe("rmrpush")
+}
+
diff --git a/pkg/sbi/nngpush_test.go b/pkg/sbi/nngpush_test.go
index d7a6572..b17b2ca 100644
--- a/pkg/sbi/nngpush_test.go
+++ b/pkg/sbi/nngpush_test.go
@@ -31,6 +31,9 @@
 	"errors"
 	"routing-manager/pkg/rtmgr"
 	"routing-manager/pkg/stub"
+	//"nanomsg.org/go/mangos/v2"
+	//_ "nanomsg.org/go/mangos/v2/transport/all"
+	//"nanomsg.org/go/mangos/v2/protocol/push"
 	"testing"
 )
 
@@ -226,3 +229,28 @@
 		t.Errorf("nngpush.DeleteEndpoint() was incorrect, got: %v, want: %v.", nil, "error")
 	}
 }
+
+/*
+Initialize and send policies
+*/
+func TestNngPushInitializeandsendPolicies(t *testing.T) {
+        var nngpush = NngPush{}
+        _,_ = createNewPushSocket()
+        policies := []string{"hello","welcome"}
+        nngpush.send(rtmgr.Eps["10.1.1.1:0"],&policies)
+}
+
+/*
+func TestPipeEventHandler(t *testing.T) {
+	var sock mangos.Socket
+	var event mangos.PipeEvent
+	var pipe mangos.Pipe
+
+	var err error
+	sock, err = push.NewSocket()
+	sock.Dial("tcp://127.0.0.1:4555")
+	sock.SetPipeEventHook(pipeEventHandler)
+        pipeEventHandler(event,pipe)
+      t.Log(err)
+}
+*/
diff --git a/pkg/sbi/sbi_test.go b/pkg/sbi/sbi_test.go
index ee8671f..52436ed 100644
--- a/pkg/sbi/sbi_test.go
+++ b/pkg/sbi/sbi_test.go
@@ -31,6 +31,7 @@
 	"errors"
 	"reflect"
 	"testing"
+	"routing-manager/pkg/rtmgr"
 )
 
 func TestGetSbi(t *testing.T) {
@@ -53,3 +54,31 @@
 		}
 	}
 }
+
+func TestUpdateE2TendPoint(t *testing.T) {
+	var err error
+	var sbi = Sbi{}
+	sbii, err := GetSbi("nngpush")
+	var E2map = make(map[string]rtmgr.E2TInstance)
+
+	E2map["1.2.3.4:100"] = rtmgr.E2TInstance{
+		 Name: "E2Tinstance1",
+	        Fqdn: "1.2.3.4:100",
+		Ranlist: []string{"1","2"},
+	}
+
+	sbi.updateE2TEndpoints(&E2map,sbii)
+	t.Log(err)
+}
+
+func TestpruneEndpointList(t *testing.T) {
+	var sbi = Sbi{}
+	var err error
+        sbii, err := GetSbi("nngpush")
+
+	var EP = make(map[string]*rtmgr.Endpoint)
+	EP["10.0.0.1:0"] = &rtmgr.Endpoint{Uuid: "10.0.0.1:0", Name: "E2TERM", XAppType: "app1", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: true}
+	rtmgr.Eps = EP
+	sbi.pruneEndpointList(sbii)
+	t.Log(err)
+}
diff --git a/pkg/sdl/sdl_test.go b/pkg/sdl/sdl_test.go
index 2d5d9db..06bc852 100644
--- a/pkg/sdl/sdl_test.go
+++ b/pkg/sdl/sdl_test.go
@@ -41,6 +41,9 @@
 
 	err = file.WriteAll("ut.rt", &stub.ValidRicComponents)
 	t.Log(err)
+	/* This test is for empty file */
+	err = file.WriteAll("", &stub.ValidRicComponents)
+	t.Log(err)
 }
 
 /*
@@ -53,4 +56,46 @@
 	data, err := file.ReadAll("ut.rt")
 	t.Log(data)
 	t.Log(err)
+	/* Test to read a Directory */
+	data, err = file.ReadAll("/tmp")
+	t.Log(data)
+	t.Log(err)
+}
+
+/*
+WriteXApps
+*/
+func TestFileWriteXApps(t *testing.T) {
+	var err error
+	var file = File{}
+
+	err = file.WriteXApps("ut.rt", stub.ValidXApps)
+	t.Log(err)
+	/*Write data to a file that doesn't exist */
+	err = file.WriteXApps("ut.rtx", stub.ValidXApps)
+	t.Log(err)
+
+}
+
+/*
+GetSdl instance with correct and incorrect arguments
+*/
+func TestFileGetSdl(t *testing.T) {
+	var err error
+	_, err = GetSdl("")
+	t.Log(err)
+	_, err = GetSdl("file")
+	t.Log(err)
+}
+
+/*
+WriteNewE2TInstance
+*/
+func TestWriteNewE2TInstance(t *testing.T) {
+	var err error
+	var file = File{}
+	file.WriteNewE2TInstance("", &stub.ValidE2TInstance)
+	t.Log(err)
+	file.WriteNewE2TInstance("ut.rt", &stub.ValidE2TInstance)
+	t.Log(err)
 }
diff --git a/pkg/stub/stub.go b/pkg/stub/stub.go
index f57f70e..40df44a 100644
--- a/pkg/stub/stub.go
+++ b/pkg/stub/stub.go
@@ -34,7 +34,7 @@
 	{Name: "app1", Status: "", Version: "", Instances: []rtmgr.XAppInstance{{Name: "E2TERM", Status: "unknown", Ip: "10.0.0.1", Port: 0, TxMessages: []string{"HandoverPreparation", "HandoverCancel"}, RxMessages: []string{"HandoverPreparation", "HandoverCancel"}}}},
 	{Name: "app2", Status: "", Version: "", Instances: []rtmgr.XAppInstance{{Name: "SUBMAN", Status: "unknown", Ip: "192.168.0.1", Port: 0, TxMessages: []string{"HandoverCancel", "HandoverPreparation"}, RxMessages: []string{"HandoverPreparation", "HandoverCancel"}}}},
 	{Name: "app3", Status: "", Version: "", Instances: []rtmgr.XAppInstance{{Name: "E2MAN", Status: "unknown", Ip: "10.1.1.1", Port: 0, TxMessages: []string{"X2Setup"}, RxMessages: []string{"Reset", "UEContextRelease"}}}},
-	{Name: "app4", Status: "", Version: "", Instances: []rtmgr.XAppInstance{{Name: "UEMAN", Status: "unknown", Ip: "10.2.2.1", Port: 0, TxMessages: []string{"Reset", "UEContextRelease"}, RxMessages: []string{"", ""}}}},
+	{Name: "app4", Status: "", Version: "", Instances: []rtmgr.XAppInstance{{Name: "UEMAN", Status: "unknown", Ip: "10.2.2.1", Port: 0, TxMessages: []string{"Reset", "UEContextRelease"}, RxMessages: []string{"", ""}, Policies: []int32{1, 2}}}},
 }
 
 var ValidPlatformComponents = &rtmgr.PlatformComponents{
@@ -46,13 +46,29 @@
 
 var ValidEndpoints = []rtmgr.Endpoint{
 	{Uuid: "10.0.0.1:0", Name: "E2TERM", XAppType: "app1", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: true},
+	{Uuid: "10.0.0.2:0", Name: "E2TERMINST", XAppType: "app2", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: true},
 	{Uuid: "192.168.0.1:0", Name: "SUBMAN", XAppType: "app2", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: false, Keepalive: false},
 	{Uuid: "10.1.1.1:0", Name: "E2MAN", XAppType: "app3", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: true, Keepalive: false},
-	{Uuid: "10.2.2.1:0", Name: "UEMAN", XAppType: "app4", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Socket: nil, IsReady: false, Keepalive: true},
+	{Uuid: "10.2.2.1:0", Name: "UEMAN", XAppType: "app4", Ip: "", Port: 0, TxMessages: []string{"", ""}, RxMessages: []string{"", ""}, Policies: []int32{1, 2}, Socket: nil, IsReady: false, Keepalive: true},
 }
 
+var ValidE2TInstance = rtmgr.E2TInstance{
+	Name:    "E2Tinstance1",
+	Fqdn:    "10.10.10.10:100",
+	Ranlist: []string{"1", "2"},
+}
+
+var E2map = make(map[string]rtmgr.E2TInstance)
+
 var ValidRicComponents = rtmgr.RicComponents{
-	XApps: *ValidXApps, Pcs: *ValidPlatformComponents,
+	XApps: *ValidXApps, Pcs: *ValidPlatformComponents, E2Ts: E2map,
 }
 
 var ValidPolicies = &[]string{"", ""}
+
+var ValidSubscriptions = &[]rtmgr.Subscription{
+	{SubID: 1234, Fqdn: "10.0.0.1", Port: 0},
+	{SubID: 1235, Fqdn: "192.168.0.1", Port: 0},
+	{SubID: 1236, Fqdn: "10.1.1.1", Port: 0},
+	{SubID: 1237, Fqdn: "10.2.2.1", Port: 0},
+}