Merge "Fix PM_MAPPER_01 test - use a non-generic log"
[integration/csit.git] / tests / dcaegen2 / testcases / resources / DMaaP.py
index 092b608..4c24561 100644 (file)
@@ -17,8 +17,6 @@ import jsonschema
 import json
 import DcaeVariables
 import SimpleHTTPServer
-from robot.api import logger
-
 
 try:
     from cStringIO import StringIO
@@ -42,10 +40,6 @@ def enque_event(evt):
     if DcaeVariables.VESEventQ is not None:
         try:
             DcaeVariables.VESEventQ.put(evt)
-            if DcaeVariables.IsRobotRun:
-                logger.console("DMaaP Event enqued - size=" + str(len(evt)))
-            else:
-                print ("DMaaP Event enqueued - size=" + str(len(evt)))
             return True
         except Exception as e:
             print (str(e))
@@ -55,18 +49,14 @@ def enque_event(evt):
 
 def deque_event(wait_sec=25):
     if DcaeVariables.IsRobotRun:
-        logger.console("Enter DequeEvent")
+        pass
     try:
         evt = DcaeVariables.VESEventQ.get(True, wait_sec)
-        if DcaeVariables.IsRobotRun:
-            logger.console("DMaaP Event dequeued - size=" + str(len(evt)))
-        else:
-            print("DMaaP Event dequeued - size=" + str(len(evt)))
         return evt
     except Exception as e:
         if DcaeVariables.IsRobotRun:
-            logger.console(str(e))
-            logger.console("DMaaP Event dequeue timeout")
+            pass
+
         else:
             print("DMaaP Event dequeue timeout")
         return None
@@ -79,7 +69,6 @@ class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         return
         
     def do_POST(self):
-        
         resp_code = 0
         # Parse the form data posted
         '''
@@ -117,26 +106,22 @@ class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         '''  
         
         if resp_code == 0:
+            topic = self.extract_topic_from_path()
             content_len = int(self.headers.getheader('content-length', 0))
             post_body = self.rfile.read(content_len)
             
-            if DcaeVariables.IsRobotRun:
-                logger.console("\n" + "DMaaP Receive Event:\n" + post_body)
-            else:
-                print("\n" + "DMaaP Receive Event:")
-                print (post_body)
-            
             indx = post_body.index("{")
             if indx != 0:
                 post_body = post_body[indx:]
-            
-            if not enque_event(post_body):
+
+            event = "\""+topic+"\":" + post_body
+            if not enque_event(event):
                 print "enque event fails"
                    
             global EvtSchema
             try:
                 if EvtSchema is None:
-                    with open(DcaeVariables.CommonEventSchemaV5) as opened_file:
+                    with open(DcaeVariables.CommonEventSchema) as opened_file:
                         EvtSchema = json.load(opened_file)
                 decoded_body = json.loads(post_body)
                 jsonschema.validate(decoded_body, EvtSchema)
@@ -168,7 +153,6 @@ class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
                 self.send_response(200)
                 self.send_header('Content-Type', 'application/json')
                 self.end_headers()
-                # self.wfile.write("{'responses' : {'200' : {'description' : 'Success'}}}")
                 self.wfile.write("{'count': 1, 'serverTimeMs': 3}")
                 self.wfile.close()
         else:
@@ -198,6 +182,9 @@ class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
         '''
         return
 
+    def extract_topic_from_path(self):
+        return self.path["/events/".__len__():]
+
     def do_GET(self):
         """Serve a GET request."""
         f = self.send_head()
@@ -386,8 +373,8 @@ class DMaaPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
 
 def test(handler_class=DMaaPHandler, server_class=BaseHTTPServer.HTTPServer, protocol="HTTP/1.0", port=3904):
-    print "Load event schema file: " + DcaeVariables.CommonEventSchemaV5
-    with open(DcaeVariables.CommonEventSchemaV5) as opened_file:
+    print "Load event schema file: " + DcaeVariables.CommonEventSchema
+    with open(DcaeVariables.CommonEventSchema) as opened_file:
         global EvtSchema
         EvtSchema = json.load(opened_file)
         
@@ -412,8 +399,8 @@ def _main_(handler_class=DMaaPHandler, server_class=BaseHTTPServer.HTTPServer, p
     else:
         port = 3904
     
-    print "Load event schema file: " + DcaeVariables.CommonEventSchemaV5
-    with open(DcaeVariables.CommonEventSchemaV5) as opened_file:
+    print "Load event schema file: " + DcaeVariables.CommonEventSchema
+    with open(DcaeVariables.CommonEventSchema) as opened_file:
         global EvtSchema
         EvtSchema = json.load(opened_file)