UT changes and fixes

Change-Id: I31bb8fcfb562bf32519f0f3bd2bbcfdae7aff728
Signed-off-by: Markku Virtanen <markku.virtanen@nokia.com>
diff --git a/Dockerfile b/Dockerfile
index 5276ea5..0a2b217 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -182,7 +182,7 @@
 RUN sed -r  "s/^(::1.*)/#\1/" /etc/hosts  > /etc/hosts.new \
     && cat /etc/hosts.new > /etc/hosts \
     && cat /etc/hosts  \
-    && go test -test.coverprofile /tmp/submgr_cover.out -count=1 -v ./pkg/control \
+    && go test -failfast -test.coverprofile /tmp/submgr_cover.out -count=1 -v ./pkg/control \
     && go tool cover -html=/tmp/submgr_cover.out -o /tmp/submgr_cover.html
 
 # test formating (not important)
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index d7f8011..93a287a 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -101,7 +101,7 @@
 			r.restSubscriptions[restSubId] = restSubscription
 			return restSubscription, nil
 		} else {
-			return restSubscription, fmt.Errorf("Registry: REST delete request is still ongoing for the endpoint=%v, restSubId=%v, SubDelReqOngoing=%v, SubReqOngoing=%v", restSubscription, restSubId, restSubscription.SubDelReqOngoing, restSubscription.SubReqOngoing)
+			return restSubscription, fmt.Errorf("Registry: REST request is still ongoing for the endpoint=%v, restSubId=%v, SubDelReqOngoing=%v, SubReqOngoing=%v", restSubscription, restSubId, restSubscription.SubDelReqOngoing, restSubscription.SubReqOngoing)
 		}
 		return restSubscription, nil
 	}
diff --git a/pkg/control/restendpoint.go b/pkg/control/restendpoint.go
index b872c47..96df505 100644
--- a/pkg/control/restendpoint.go
+++ b/pkg/control/restendpoint.go
@@ -22,6 +22,7 @@
 import (
 	"fmt"
 	"strconv"
+	"strings"
 
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
 	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
@@ -38,12 +39,6 @@
 	var xAppHTTPEndPoint string
 	var xAppRMREndPoint string
 
-	if *clientEndpoint.HTTPPort > 0 {
-		xAppHTTPEndPoint = host + ":" + strconv.FormatInt(*clientEndpoint.HTTPPort, 10)
-	}
-	if *clientEndpoint.RMRPort > 0 {
-		xAppRMREndPoint = host + ":" + strconv.FormatInt(*clientEndpoint.RMRPort, 10)
-	}
 	if host == "" || (HTTP_port == 0 && RMR_port == 0) {
 		err := fmt.Errorf("ClientEndpoint aprovided no PORT numbers")
 		return "INVALID_HTTP_ADDRESS:" + host + (string)(*clientEndpoint.HTTPPort),
@@ -51,6 +46,16 @@
 			err
 	}
 
+	if *clientEndpoint.HTTPPort > 0 {
+		xAppHTTPEndPoint = host + ":" + strconv.FormatInt(*clientEndpoint.HTTPPort, 10)
+	}
+	if *clientEndpoint.RMRPort > 0 {
+		if i := strings.Index(host, "http"); i != -1 {
+			host = strings.Replace(host, "http", "rmr", -1)
+		}
+		xAppRMREndPoint = host + ":" + strconv.FormatInt(*clientEndpoint.RMRPort, 10)
+	}
+
 	xapp.Logger.Info("xAppHttpEndPoint=%v, xAppRrmEndPoint=%v", xAppHTTPEndPoint, xAppRMREndPoint)
 
 	return xAppHTTPEndPoint, xAppRMREndPoint, nil
diff --git a/pkg/control/ut_ctrl_submgr_test.go b/pkg/control/ut_ctrl_submgr_test.go
index 3a0e0e4..9ffda38 100644
--- a/pkg/control/ut_ctrl_submgr_test.go
+++ b/pkg/control/ut_ctrl_submgr_test.go
@@ -170,6 +170,35 @@
 	return false
 }
 
+func (mc *testingSubmgrControl) wait_multi_subs_clean(t *testing.T, e2SubsIds []uint32, secs int) bool {
+	var subs *Subscription
+	var purgedSubscriptions int
+	i := 1
+	k := 0
+	for ; i <= secs*2; i++ {
+		purgedSubscriptions = 0
+		for k = 0; k <= len(e2SubsIds); i++ {
+			subs = mc.c.registry.GetSubscription(e2SubsIds[k])
+			if subs == nil {
+				mc.TestLog(t, "(submgr) subscriber purged for esSubsId %v", e2SubsIds[k])
+				purgedSubscriptions += 1
+				if purgedSubscriptions == len(e2SubsIds) {
+					return true
+				} else {
+					continue
+				}
+			} else {
+				mc.TestLog(t, "(submgr) subscriber %s no clean within %d secs: subs(N/A) - purged subscriptions %v", subs.String(), secs, purgedSubscriptions)
+				time.Sleep(500 * time.Millisecond)
+			}
+		}
+	}
+
+	mc.TestError(t, "(submgr) no clean within %d secs: subs(N/A) - %v/%v subscriptions found still", secs, purgedSubscriptions, len(e2SubsIds))
+
+	return false
+}
+
 func (mc *testingSubmgrControl) wait_subs_trans_clean(t *testing.T, e2SubsId uint32, secs int) bool {
 	var trans TransactionIf
 	i := 1
diff --git a/pkg/control/ut_messaging_test.go b/pkg/control/ut_messaging_test.go
index 415c438..b91b4b8 100644
--- a/pkg/control/ut_messaging_test.go
+++ b/pkg/control/ut_messaging_test.go
@@ -2245,16 +2245,16 @@
 	const parameterSet = 1
 	const actionDefinitionPresent bool = true
 	const actionParamCount int = 1
-	waiter := rtmgrHttp.AllocNextEvent(false)
+	// Add delay for rtmgt HTTP handling so that HTTP response is received before notify on XAPP side
+	waiter := rtmgrHttp.AllocNextSleep(50, false)
 	newSubsId := mainCtrl.get_registry_next_subid(t)
 
 	// Req
 	params := xappConn1.GetRESTSubsReqReportParams(subReqCount, parameterSet, actionDefinitionPresent, actionParamCount)
 	restSubId := xappConn1.SendRESTSubsReq(t, params)
-
+	xappConn1.ExpectRESTNotification(t, restSubId)
 	waiter.WaitResult(t)
 
-	xappConn1.ExpectRESTNotification(t, restSubId)
 	e2SubsId := xappConn1.WaitRESTNotification(t, restSubId)
 	xapp.Logger.Info("TEST: REST notification received e2SubsId=%v", e2SubsId)
 
@@ -2424,7 +2424,7 @@
 //     |                 |              |
 //
 //-----------------------------------------------------------------------------
-/*
+
 func TestRESTSubReqRetransmission(t *testing.T) {
 	CaseBegin("TestRESTSubReqRetransmission")
 
@@ -2476,19 +2476,18 @@
 	delreq1, delmsg1 := e2termConn1.RecvSubsDelReq(t)
 	e2termConn1.SendSubsDelResp(t, delreq1, delmsg1)
 
-	// Wait that subs is cleaned
-	mainCtrl.wait_subs_clean(t, e2SubsIdA.E2SubsId, 10)
-
 	// Del2
 	xappConn2.SendRESTSubsDelReq(t, &restSubId2)
 	delreq2, delmsg2 := e2termConn1.RecvSubsDelReq(t)
 	e2termConn1.SendSubsDelResp(t, delreq2, delmsg2)
 
+	mainCtrl.wait_multi_subs_clean(t, []uint32{e2SubsIdA.E2SubsId, e2SubsIdB.E2SubsId}, 10)
+
 	waitSubsCleanup(t, e2SubsIdB.E2SubsId, 10)
 
 	mainCtrl.VerifyCounterValues(t)
 }
-*/
+
 func TestRESTSubDelReqRetransmission(t *testing.T) {
 	CaseBegin("TestRESTSubDelReqRetransmission")
 
@@ -3448,15 +3447,14 @@
 	e2termConn1.RecvSubsReq(t)
 
 	delreq1, delmsg1 := e2termConn1.RecvSubsDelReq(t)
-	xappConn1.ExpectRESTNotification(t, restSubId1) // or restSubId2?
-	//	xappConn2.WaitRESTNotification(t)
+
+	xappConn1.WaitListedRestNotifications(t, []string{restSubId1, restSubId2})
 	e2termConn1.SendSubsDelResp(t, delreq1, delmsg1)
 
-	e2SubsId1 := xappConn1.WaitRESTNotification(t, restSubId1)
-	xapp.Logger.Info("TEST: REST notification received e2SubsId=%v", e2SubsId1)
-
-	//	e2SubsId2 := <-xappConn2.RESTNotification
-	//	xapp.Logger.Info("TEST: REST notification received e2SubsId=%v", e2SubsId2)
+	e2SubsIdA := <-xappConn1.ListedRESTNotifications
+	xapp.Logger.Info("TEST: 1.st XAPP notification received e2SubsId=%v", e2SubsIdA)
+	e2SubsIdB := <-xappConn1.ListedRESTNotifications
+	xapp.Logger.Info("TEST: 2.nd XAPP notification received e2SubsId=%v", e2SubsIdB)
 
 	// Del1
 	xappConn1.SendRESTSubsDelReq(t, &restSubId1)
@@ -3464,8 +3462,10 @@
 	// Del2
 	xappConn2.SendRESTSubsDelReq(t, &restSubId2)
 
+	mainCtrl.wait_multi_subs_clean(t, []uint32{e2SubsIdA.E2SubsId, e2SubsIdB.E2SubsId}, 10)
+
 	//Wait that subs is cleaned
-	waitSubsCleanup(t, e2SubsId1, 10)
+	waitSubsCleanup(t, e2SubsIdA.E2SubsId, 10)
 
 	mainCtrl.VerifyCounterValues(t)
 }
@@ -4336,7 +4336,6 @@
 	mainCtrl.wait_registry_empty(t, 10)
 }
 
-/*
 func TestRESTTwoPolicySubReqAndSubDelOk(t *testing.T) {
 
 	subReqCount := 2
@@ -4369,8 +4368,6 @@
 
 	mainCtrl.VerifyCounterValues(t)
 }
-*/
-/*
 func TestRESTPolicySubReqAndSubDelOkFullAmount(t *testing.T) {
 
 	subReqCount := 19
@@ -4402,8 +4399,6 @@
 
 	mainCtrl.VerifyCounterValues(t)
 }
-*/
-/*
 func TestRESTTwoReportSubReqAndSubDelOk(t *testing.T) {
 
 	subReqCount := 2
@@ -4439,7 +4434,7 @@
 
 	mainCtrl.VerifyCounterValues(t)
 }
-*/
+
 /*
 func TestRESTTwoReportSubReqAndSubDelOkNoActParams(t *testing.T) {
 
diff --git a/pkg/control/ut_restendpoint_test.go b/pkg/control/ut_restendpoint_test.go
new file mode 100644
index 0000000..cabae3c
--- /dev/null
+++ b/pkg/control/ut_restendpoint_test.go
@@ -0,0 +1,144 @@
+/*
+==================================================================================
+  Copyright (c) 2021 Nokia
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================================
+*/
+
+package control
+
+import (
+	"testing"
+
+	"gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
+)
+
+func TestRestEndpointOk(t *testing.T) {
+
+	var clientEndpoint models.SubscriptionParamsClientEndpoint
+	var httpPort int64
+	var rmrPort int64
+	var host string
+
+	httpPort = 8080
+	rmrPort = 4560
+	host = "service-ricxapp-xappname-http.ricxapp"
+
+	clientEndpoint.HTTPPort = &httpPort
+	clientEndpoint.RMRPort = &rmrPort
+	clientEndpoint.Host = host
+
+	expectedHttpEndpoint := "service-ricxapp-xappname-http.ricxapp:8080"
+	expectedRmrEndpoint := "service-ricxapp-xappname-rmr.ricxapp:4560"
+
+	httpEndPoint, rmrEndPoint, err := ConstructEndpointAddresses(clientEndpoint)
+
+	if err != nil {
+		t.Errorf("Mismatching return value: %s - ecpected NIL", err)
+	}
+	if httpEndPoint != expectedHttpEndpoint {
+		t.Errorf("Mismatching httpEndpoint: %s - ecpected %s", httpEndPoint, expectedHttpEndpoint)
+	}
+	if rmrEndPoint != expectedRmrEndpoint {
+		t.Errorf("Mismatching httpEndpoint: %s - ecpected %s", httpEndPoint, expectedHttpEndpoint)
+	}
+}
+
+func TestRestEndpointNoHttpPort(t *testing.T) {
+
+	var clientEndpoint models.SubscriptionParamsClientEndpoint
+	var httpPort int64
+	var rmrPort int64
+	var host string
+
+	httpPort = 0
+	rmrPort = 4561
+	host = "service-ricxapp-xappname-http.ricxapp"
+
+	clientEndpoint.HTTPPort = &httpPort
+	clientEndpoint.RMRPort = &rmrPort
+	clientEndpoint.Host = host
+
+	expectedHttpEndpoint := ""
+	expectedRmrEndpoint := "service-ricxapp-xappname-rmr.ricxapp:4561"
+
+	httpEndPoint, rmrEndPoint, err := ConstructEndpointAddresses(clientEndpoint)
+
+	if err != nil {
+		t.Errorf("Mismatching return value: %s - ecpected NIL", err)
+	}
+	if httpEndPoint != expectedHttpEndpoint {
+		t.Errorf("Mismatching httpEndpoint: %s - ecpected %s", httpEndPoint, expectedHttpEndpoint)
+	}
+	if rmrEndPoint != expectedRmrEndpoint {
+		t.Errorf("Mismatching httpEndpoint: %s - ecpected %s", httpEndPoint, expectedHttpEndpoint)
+	}
+}
+
+func TestRestEndpointNok(t *testing.T) {
+
+	var clientEndpoint models.SubscriptionParamsClientEndpoint
+	var httpPort int64
+	var rmrPort int64
+	var host string
+
+	httpPort = 0
+	rmrPort = 0
+	host = "service-ricxapp-xappname-http.ricxapp"
+
+	clientEndpoint.HTTPPort = &httpPort
+	clientEndpoint.RMRPort = &rmrPort
+	clientEndpoint.Host = host
+
+	_, _, err := ConstructEndpointAddresses(clientEndpoint)
+
+	if err == nil {
+		t.Errorf("Mismatching return value: - expected ERR but got NIL")
+	}
+}
+
+func TestRestEndpointNoHttpInHost(t *testing.T) {
+
+	var clientEndpoint models.SubscriptionParamsClientEndpoint
+	var httpPort int64
+	var rmrPort int64
+	var host string
+
+	httpPort = 8080
+	rmrPort = 4562
+	host = "service-ricxapp-xappname.ricxapp"
+
+	clientEndpoint.HTTPPort = &httpPort
+	clientEndpoint.RMRPort = &rmrPort
+	clientEndpoint.Host = host
+
+	expectedHttpEndpoint := "service-ricxapp-xappname.ricxapp:8080"
+	expectedRmrEndpoint := "service-ricxapp-xappname.ricxapp:4562"
+
+	httpEndPoint, rmrEndPoint, err := ConstructEndpointAddresses(clientEndpoint)
+
+	if err != nil {
+		t.Errorf("Mismatching return value: %s - ecpected NIL", err)
+	}
+	if httpEndPoint != expectedHttpEndpoint {
+		t.Errorf("Mismatching httpEndpoint: %s - ecpected %s", httpEndPoint, expectedHttpEndpoint)
+	}
+	if rmrEndPoint != expectedRmrEndpoint {
+		t.Errorf("Mismatching httpEndpoint: %s - ecpected %s", httpEndPoint, expectedHttpEndpoint)
+	}
+}
+
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
diff --git a/pkg/teststube2ap/stubE2.go b/pkg/teststube2ap/stubE2.go
index b8d2350..4c8c08d 100644
--- a/pkg/teststube2ap/stubE2.go
+++ b/pkg/teststube2ap/stubE2.go
@@ -764,7 +764,7 @@
 //
 //-----------------------------------------------------------------------------
 func (tc *E2Stub) SendRESTSubsReq(t *testing.T, params *RESTSubsReqParams) string { // This need to be edited according to new specification
-	tc.Info("Posting REST Report subscriptions to Submgr")
+	tc.Info("======== Posting REST Report subscriptions to Submgr ======")
 
 	if params == nil {
 		tc.Info("SendRESTReportSubsReq: params == nil")
@@ -775,10 +775,11 @@
 	if err != nil {
 		// Swagger generated code makes checks for the values that are inserted the subscription function
 		// If error cause is unknown and POST is not done, the problem is in the inserted values
-		tc.Error("REST report subscriptions failed %s", err.Error())
+		tc.Error("======== REST report subscriptions failed %s ========", err.Error())
+		return ""
 	}
 	tc.subscriptionId = *resp.SubscriptionID
-	tc.Info("REST report subscriptions pushed successfully. SubscriptionID = %s, RequestCount = %v", *resp.SubscriptionID, tc.requestCount)
+	tc.Info("======== REST report subscriptions posted successfully. SubscriptionID = %s, RequestCount = %v ========", *resp.SubscriptionID, tc.requestCount)
 	return *resp.SubscriptionID
 }