HelloWorld E2SM unit tests

Issue-ID: RICAPP-67

Signed-off-by: sjana <sj492a@att.com>
Change-Id: I49412e31f7d54de1a7942dba579e190b631a2dfb
diff --git a/test/Makefile b/test/Makefile
index 0fd285f..ec89c05 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -7,14 +7,14 @@
 MGMTSRC:=../src/xapp-mgmt
 
 ASNSRC:=../asn1c_defs
-E2APSRC:=../src/xapp-formats/e2ap
-E2SMSRC:=../src/xapp-formats/e2sm
+E2APSRC:=../src/xapp-asn/e2ap
+E2SMSRC:=../src/xapp-asn/e2sm
 
 ####### Logging library and flags
 CLOGFLAGS:= `pkg-config mdclog --cflags`
 LOG_LIBS:= `pkg-config mdclog --libs`
 CURL_LIBS:= `pkg-config libcurl --libs`
-RNIB_LIBS:= -pthread /usr/local/lib/rnibreader_old.a
+RNIB_LIBS:= -pthread /usr/local/include/rnib/rnibreader.a
 GTEST_LIBS:= -lgtest -lgtest_main
 ######## Keep include dirs separate so we have transparency
 
diff --git a/test/hw_unit_tests.cc b/test/hw_unit_tests.cc
index d6c9339..99187c5 100644
--- a/test/hw_unit_tests.cc
+++ b/test/hw_unit_tests.cc
@@ -1,7 +1,7 @@
 /*
 ==================================================================================
 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+        Copyright (c) 2019-2020 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
@@ -27,20 +27,41 @@
 #include<gtest/gtest.h>
 
 #include "test_db.h"
-//#include "test_rmr.h"
+#include "test_rmr.h"
 #include "test_hc.h"
-
-#define HC_MSG_SIZE 512
+#include "test_subs.h"
+#include "test_e2sm.h"
 
 using namespace std;
 
 
 int main(int argc, char* argv[])
 {
-//	char rmr_seed[80]="RMR_SEED_RT=../src/routes.txt";
-	char rmr_seed[80]="/tmp/routeinfo/routes.txt";
-	putenv(rmr_seed);
+	char *aux;
+	aux=getenv("RMR_SEED_RT");
+	if (aux==NULL || *aux == '\0'){
+
+		char rmr_seed[80]="RMR_SEED_RT=../init/routes.txt";
+		putenv(rmr_seed);
+	}
+	//get configuration
+	XappSettings config;
+	//change the priority depending upon application requirement
+	config.loadDefaultSettings();
+	config.loadEnvVarSettings();
+
+	//initialize rmr
+	std::unique_ptr<XappRmr> rmr = std::make_unique<XappRmr>("38000");
+	rmr->xapp_rmr_init(true);
+
+	//create a dummy xapp
+	std::unique_ptr<Xapp> dm_xapp = std::make_unique<Xapp>(std::ref(config),std::ref(*rmr));
+	dm_xapp->Run();
 
 	testing::InitGoogleTest(&argc, argv);
-	return RUN_ALL_TESTS();
+	int res = RUN_ALL_TESTS();
+
+
+
+	return res;
 }
diff --git a/test/routes.txt b/test/routes.txt
new file mode 100644
index 0000000..02fb39f
--- /dev/null
+++ b/test/routes.txt
@@ -0,0 +1,13 @@
+newrt|start
+rte|100|127.0.0.1:4560
+rte|101|127.0.0.1:38000
+rte|20010|127.0.0.1:4560
+rte|20011|127.0.0.1:38000
+rte|20012|127.0.0.1:4560
+rte|12010|127.0.0.1:38000
+rte|12011|127.0.0.1:4560
+rte|12012|127.0.0.1:4560
+rte|12020|127.0.0.1:38000
+rte|12021|127.0.0.1:4560
+rte|12022|127.0.0.1:4560
+newrt|end
diff --git a/test/test_db.h b/test/test_db.h
index e7716b4..478af92 100644
--- a/test/test_db.h
+++ b/test/test_db.h
@@ -1,7 +1,7 @@
 /*
 ==================================================================================
 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+        Copyright (c) 2019-2020 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
@@ -38,7 +38,6 @@
 
 	Xapp hw_xapp(std::ref(config),rmr);
 	hw_xapp.set_rnib_gnblist();
-
 	auto gnblist = hw_xapp.get_rnib_gnblist();
 	int sz = gnblist.size();
 	EXPECT_GE(sz,0);
diff --git a/test/test_e2sm.h b/test/test_e2sm.h
new file mode 100644
index 0000000..2300303
--- /dev/null
+++ b/test/test_e2sm.h
@@ -0,0 +1,189 @@
+/*
+==================================================================================
+
+        Copyright (c) 2019-2020 AT&T Intellectual Property.
+
+   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.
+==================================================================================
+/*
+ * test_asn.h
+ *
+ *  Created on: Apr, 2020
+ *      Author: Shraboni Jana
+ */
+
+#ifndef TEST_TEST_ASN_H_
+#define TEST_TEST_ASN_H_
+#include<iostream>
+#include<gtest/gtest.h>
+
+#include "subscription_request.hpp"
+#include "xapp.hpp"
+#include "e2sm_helpers.hpp"
+#include "e2sm_subscription.hpp"
+#include "e2sm_indication.hpp"
+#include "e2sm_control.hpp"
+
+using namespace std;
+TEST(E2SM, SubscriptionRequest)
+{
+
+	unsigned char event_buf[128];
+	size_t event_buf_len = 128;
+
+	unsigned char act_buf[128];
+	size_t act_buf_len = 128;
+
+	bool res;
+
+
+	e2sm_subscription_helper e2sm_subsdata;
+	std::unique_ptr<ranparam_helper> *ranhelp;
+	e2sm_subscription e2sm_subs;
+
+
+	e2sm_subsdata.triger_nature = 0;
+
+	int param_id = 1;
+	unsigned char param_name[20];
+	strcpy((char*)param_name,"ParamName");
+	int param_name_len = strlen((const char*)param_name);
+
+	int param_test = 0;
+	unsigned char param_value[20];
+	strcpy((char*)param_value,"ParamValue");
+	int param_value_len = strlen((const char*)param_value);
+
+	e2sm_subsdata.add_param(param_id, param_name, param_name_len, param_test, param_value, param_value_len);
+
+
+	// Encode the event trigger definition
+	res = e2sm_subs.encode_event_trigger(&event_buf[0], &event_buf_len, e2sm_subsdata);
+	if(!res)
+		std::cout << e2sm_subs.get_error() << std::endl;
+
+	ASSERT_TRUE(res);
+
+	// Encode the action defintion
+	res = e2sm_subs.encode_action_defn(&act_buf[0], &act_buf_len, e2sm_subsdata);
+	if(!res)
+		std::cout << e2sm_subs.get_error() << std::endl;
+	ASSERT_TRUE(res);
+
+}
+TEST(E2SM, IndicationMessage)
+{
+
+	unsigned char header_buf[128];
+	size_t header_buf_len = 128;
+
+	unsigned char msg_buf[128];
+	size_t msg_buf_len = 128;
+
+	bool res;
+	asn_dec_rval_t retval;
+
+
+	e2sm_indication_helper e2sm_inddata;
+	e2sm_indication e2sm_inds;
+
+	unsigned char msg[20] = "HelloWorld";
+
+	e2sm_inddata.header = 1001;
+	e2sm_inddata.message = msg;
+	e2sm_inddata.message_len = strlen((const char*)e2sm_inddata.message);
+
+
+	// Encode the indication header
+	res = e2sm_inds.encode_indication_header(&header_buf[0], &header_buf_len, e2sm_inddata);
+	if(!res)
+		std::cout << e2sm_inds.get_error() << std::endl;
+
+	ASSERT_TRUE(res);
+
+	// Encode the indication message
+	res = e2sm_inds.encode_indication_message(&msg_buf[0], &msg_buf_len, e2sm_inddata);
+	if(!res)
+		std::cout << e2sm_inds.get_error() << std::endl;
+	ASSERT_TRUE(res);
+
+	//decode the indication header
+	e2sm_indication_helper e2sm_decodedata;
+
+
+	E2SM_HelloWorld_IndicationHeader_t *header = 0; // used for decoding
+	retval = asn_decode(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_HelloWorld_IndicationHeader, (void**)&(header), &header_buf[0], header_buf_len);
+
+	ASSERT_TRUE(retval.code == RC_OK);
+	res = e2sm_inds.get_fields(header, e2sm_decodedata);
+
+	//decode the indication message
+
+	E2SM_HelloWorld_IndicationMessage_t *mesg = 0; // used for decoding
+	retval = asn_decode(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_HelloWorld_IndicationMessage, (void**)&(mesg), &msg_buf[0], msg_buf_len);
+
+	ASSERT_TRUE(retval.code == RC_OK);
+	res = e2sm_inds.get_fields(mesg, e2sm_decodedata);
+
+
+	std::cout << "Indication Header:" << e2sm_decodedata.header << std::endl;
+	std::cout << "Indication Message:" << e2sm_decodedata.message << std::endl;
+	std::cout << "Indication Message Len:" << e2sm_decodedata.message_len << std::endl;
+
+	ASSERT_EQ(e2sm_inddata.header, e2sm_decodedata.header);
+	ASSERT_EQ(e2sm_inddata.message_len, e2sm_decodedata.message_len);
+	for (int i = 0; i < e2sm_inddata.message_len; ++i) {
+	  EXPECT_EQ(e2sm_inddata.message[i], e2sm_decodedata.message[i]) << "Encoded and Decoded Msg differ at index " << i;
+	}
+
+
+}
+
+TEST(E2SM, ControlMessage)
+{
+
+	unsigned char header_buf[128];
+	size_t header_buf_len = 128;
+
+	unsigned char msg_buf[128];
+	size_t msg_buf_len = 128;
+
+	bool res;
+	asn_dec_rval_t retval;
+
+
+	e2sm_control_helper e2sm_cntrldata;
+	e2sm_control e2sm_cntrl;
+
+	unsigned char msg[20] = "HelloWorld";
+
+	e2sm_cntrldata.header = 1001;
+	e2sm_cntrldata.message = msg;
+	e2sm_cntrldata.message_len = strlen((const char*)e2sm_cntrldata.message);
+
+
+	// Encode the indication header
+	res = e2sm_cntrl.encode_control_header(&header_buf[0], &header_buf_len, e2sm_cntrldata);
+	if(!res)
+		std::cout << e2sm_cntrl.get_error() << std::endl;
+
+	ASSERT_TRUE(res);
+
+	// Encode the indication message
+	res = e2sm_cntrl.encode_control_message(&msg_buf[0], &msg_buf_len, e2sm_cntrldata);
+	if(!res)
+		std::cout << e2sm_cntrl.get_error() << std::endl;
+	ASSERT_TRUE(res);
+}
+
+#endif /* TEST_TEST_ASN_H_ */
diff --git a/test/test_hc.h b/test/test_hc.h
index cbb8726..d07fbfe 100644
--- a/test/test_hc.h
+++ b/test/test_hc.h
@@ -1,7 +1,7 @@
 /*
 ==================================================================================
 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+        Copyright (c) 2019-2020 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
 #include "xapp.hpp"
 #define HC_MSG_SIZE 512
 
+
 using namespace std;
 TEST(Xapp, RMRHealthCheck){
 
@@ -38,7 +39,7 @@
 
 	 std::unique_ptr<XappRmr> rmr;
 	 rmr = std::make_unique<XappRmr>("4560",num_attempts);
-	 rmr->xapp_rmr_init();
+	 rmr->xapp_rmr_init(true);
 
 	 XappSettings config;
 
@@ -61,10 +62,10 @@
 		 bool res = rmr->xapp_rmr_send(&hdr,(void*)strMsg);
 		 usleep(1);
 	 }
-	 sleep(10);
+	 sleep(2);
 	 hw_xapp->stop();
-	 sleep(10);
- };
+
+};
 
 TEST(Xapp, A1HealthCheck){
 
@@ -80,7 +81,7 @@
 
 	std::unique_ptr<XappRmr> rmr;
 	rmr = std::make_unique<XappRmr>("4560",num_attempts);
-	rmr->xapp_rmr_init();
+	rmr->xapp_rmr_init(true);
 
 	XappSettings config;
 
@@ -106,8 +107,6 @@
 	bool res_msg2 = rmr->xapp_rmr_send(&hdr,(void*)strMsg);
 	ASSERT_TRUE(res_msg2);
 
-	sleep(10);
+	sleep(2);
 	hw_xapp->stop();
-	sleep(10);
-
 }
diff --git a/test/test_rmr.h b/test/test_rmr.h
index a85c8c5..fc4bf86 100644
--- a/test/test_rmr.h
+++ b/test/test_rmr.h
@@ -1,7 +1,7 @@
 /*
 ==================================================================================
 
-        Copyright (c) 2018-2019 AT&T Intellectual Property.
+        Copyright (c) 2019-2020 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
@@ -28,8 +28,6 @@
 #include<gtest/gtest.h>
 #include "xapp.hpp"
 
-#define HC_MSG_SIZE 512
-
 using namespace std;
 
 TEST(RMR, Sender){
@@ -39,13 +37,12 @@
 
 	std::unique_ptr<XappRmr> rmr;
 	rmr = std::make_unique<XappRmr>("4560",num_attempts);
-	rmr->xapp_rmr_init();
-	rmr->set_listen(false);
+	rmr->xapp_rmr_init(false);
 
 	xapp_rmr_header hdr;
  	hdr.message_type = RIC_HEALTH_CHECK_REQ;
 
-    for(int i = 0; i < total_num_msgs; i++){
+ 	for(int i = 0; i < total_num_msgs; i++){
     	std::string temp="HelloWorld: RMR Health Check"+ std::to_string(i);
     	int n = temp.length();
     	char strMsg[n+1];
@@ -64,12 +61,47 @@
 	 //initialize rmr
 	 std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>("HW-Xapp-id");
 	 XappRmr *rmr = new XappRmr("4560");
-	 rmr->xapp_rmr_init();
+	 rmr->xapp_rmr_init(false);
 	 sleep(10);
-	 rmr->set_listen(false);
+
 	 rmr->xapp_rmr_receive(std::move(*mp_handler), rmr);
 	 ASSERT_TRUE(true);
  };
 
+TEST(RMR, SendReceiveWithMEID) {
+    XappSettings config;
+	int total_num_msgs = 2;
+	int num_attempts = 10;
 
+	std::unique_ptr<XappRmr> rmr;
+	rmr = std::make_unique<XappRmr>("4560",num_attempts);
+	rmr->xapp_rmr_init(true);
+
+	std::unique_ptr<Xapp> hw_xapp = std::make_unique<Xapp>(config,std::ref(*rmr));
+	std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>("HW-Xapp_ID");
+	hw_xapp->start_xapp_receiver(std::ref(*mp_handler));
+
+	xapp_rmr_header hdr;
+	//can be any message type. using as an example
+	hdr.message_type = RIC_SUB_RESP;
+	memset(hdr.meid,0,32);
+	string meid = "NYC123";
+	strcpy(reinterpret_cast<char*>(hdr.meid), meid.c_str());
+
+ 	for(int i = 0; i < total_num_msgs; i++){
+    	std::string temp="HelloWorld: RMR Health Check"+ std::to_string(i);
+    	int n = temp.length();
+    	char strMsg[n+1];
+        std::strcpy(strMsg,temp.c_str());
+
+        clock_gettime(CLOCK_REALTIME, &(hdr.ts));
+        hdr.payload_length = n+1;
+
+        bool res = rmr->xapp_rmr_send(&hdr,(void*)strMsg);
+        ASSERT_TRUE(res);
+        usleep(1);
+     }
+ 	  sleep(1);
+      hw_xapp->stop();
+}
 #endif /* TEST_TEST_RMR_H_ */
diff --git a/test/test_subs.h b/test/test_subs.h
new file mode 100644
index 0000000..2c33d55
--- /dev/null
+++ b/test/test_subs.h
@@ -0,0 +1,64 @@
+/*
+==================================================================================
+
+        Copyright (c) 2019-2020 AT&T Intellectual Property.
+
+   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.
+==================================================================================
+*/
+#ifndef TEST_TEST_SUBS_H_
+#define TEST_TEST_SUBS_H_
+
+#include<iostream>
+#include<gtest/gtest.h>
+#include "xapp.hpp"
+#define BUFFER_SIZE 1024
+
+using namespace std;
+//generating a E2AP Subscription Message
+TEST(SUBSCRIPTION, Request){
+
+
+	subscription_helper  din;
+	subscription_helper  dout;
+
+	subscription_request sub_req;
+	subscription_request sub_recv;
+
+	unsigned char buf[BUFFER_SIZE];
+	size_t buf_size = BUFFER_SIZE;
+	bool res;
+
+
+	//Random Data  for request
+	int request_id = 1;
+	int function_id = 0;
+	std::string event_def = "HelloWorld Event Definition";
+
+	din.set_request(request_id);
+	din.set_function_id(function_id);
+	din.set_event_def(event_def.c_str(), event_def.length());
+
+	std::string act_def = "HelloWorld Action Definition";
+
+	din.add_action(1,1,(void*)act_def.c_str(), act_def.length(), 0);
+
+	res = sub_req.encode_e2ap_subscription(&buf[0], &buf_size, din);
+	ASSERT_TRUE(res);
+
+
+
+}
+
+
+#endif /* TEST_TEST_SUBS_H_ */
diff --git a/test/xapp_env.sh b/test/xapp_env.sh
new file mode 100755
index 0000000..fe297a5
--- /dev/null
+++ b/test/xapp_env.sh
@@ -0,0 +1,14 @@
+#! /bin/bash
+
+export RMR_SEED_RT="../init/routes.txt"
+export RMR_RTG_SVC="9999"
+export MSG_MAX_BUFFER="2072"
+export THREADS="1"
+export VERBOSE="0"
+export CONFIG_FILE="config/config-file.json"
+export XAPP_ID="3489-er492k-92389"
+export LOG_LEVEL="MDCLOG_ERR"
+export DBAAS_SERVICE_HOST="127.0.0.1"
+export DBAAS_SERVICE_PORT="6379"
+
+