Merge "tests for ssl connection for PRH, AAI and DmaaP"
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py b/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
index 76823b0..416e7f4 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/AAI.py
@@ -1,9 +1,7 @@
-import _thread
 import re
-import ssl
 import time
 from http.server import BaseHTTPRequestHandler
-from http.server import HTTPServer
+import httpServerLib
 
 pnfs = 'Empty'
 
@@ -15,7 +13,7 @@
             global pnfs
             content_length = int(self.headers['Content-Length'])
             pnfs = self.rfile.read(content_length)
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
@@ -23,10 +21,11 @@
         if re.search('/reset', self.path):
             global pnfs
             pnfs = 'Empty'
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
+
 class AAIHandler(BaseHTTPRequestHandler):
 
     def do_PATCH(self):
@@ -41,37 +40,14 @@
         return
 
 
-def _header_200_and_json(self):
-    self.send_response(200)
-    self.send_header('Content-Type', 'application/json')
-    self.end_headers()
-
-
 def _main_(handler_class=AAIHandler, protocol="HTTP/1.0"):
     handler_class.protocol_version = protocol
-    _thread.start_new_thread(_init_http_endpoints, (3333, AAIHandler))
-    _thread.start_new_thread(_init_https_endpoints, (3334, AAIHandler))
-    _thread.start_new_thread(_init_http_endpoints, (3335, AAISetup))
+    httpServerLib.start_http_endpoint(3333, AAIHandler)
+    httpServerLib.start_https_endpoint(3334, AAIHandler)
+    httpServerLib.start_http_endpoint(3335, AAISetup)
     while 1:
         time.sleep(10)
 
 
-def _init_http_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    sa = server.socket.getsockname()
-    print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
-def _init_https_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    server.socket = ssl.wrap_socket(server.socket,
-                                    keyfile="certs/server.key", certfile="certs/server.crt",
-                                    ca_certs="certs/client.crt", server_side=True)
-    sa = server.socket.getsockname()
-    print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
 if __name__ == '__main__':
     _main_()
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/AAI_simulator b/tests/dcaegen2/prh-testcases/resources/simulator/AAI_simulator
index 7364769..a906bc5 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/AAI_simulator
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/AAI_simulator
@@ -1,6 +1,6 @@
 FROM python:3-alpine
 
-ADD AAI.py /
+ADD AAI.py httpServerLib.py /
 COPY certs/* /certs/
 
 CMD [ "python", "./AAI.py" ]
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py b/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
index 3ff951e..bb37ddd 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP.py
@@ -1,9 +1,7 @@
-import _thread
 import re
-import ssl
 import time
 from http.server import BaseHTTPRequestHandler
-from http.server import HTTPServer
+import httpServerLib
 
 posted_event_from_prh = b'Empty'
 received_event_to_get_method = b'Empty'
@@ -16,13 +14,13 @@
             global received_event_to_get_method
             content_length = int(self.headers['Content-Length'])
             received_event_to_get_method = self.rfile.read(content_length)
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
     def do_GET(self):
         if re.search('/events/pnfReady', self.path):
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
             self.wfile.write(posted_event_from_prh)
 
         return
@@ -33,7 +31,7 @@
             global received_event_to_get_method
             posted_event_from_prh = b'Empty'
             received_event_to_get_method = b'Empty'
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
@@ -45,49 +43,26 @@
             global posted_event_from_prh
             content_length = int(self.headers['Content-Length'])
             posted_event_from_prh = self.rfile.read(content_length)
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
 
         return
 
     def do_GET(self):
         if re.search('/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDcae-c12/c12', self.path):
-            _header_200_and_json(self)
+            httpServerLib.header_200_and_json(self)
             self.wfile.write(received_event_to_get_method)
 
         return
 
 
-def _header_200_and_json(self):
-    self.send_response(200)
-    self.send_header('Content-Type', 'application/json')
-    self.end_headers()
-
-
 def _main_(handler_class=DMaaPHandler, protocol="HTTP/1.0"):
     handler_class.protocol_version = protocol
-    _thread.start_new_thread(_init_http_endpoints, (2222, DMaaPHandler))
-    _thread.start_new_thread(_init_https_endpoints, (2223, DMaaPHandler))
-    _thread.start_new_thread(_init_http_endpoints, (2224, DmaapSetup))
+    httpServerLib.start_http_endpoint(2222, DMaaPHandler)
+    httpServerLib.start_https_endpoint(2223, DMaaPHandler)
+    httpServerLib.start_http_endpoint(2224, DmaapSetup)
     while 1:
         time.sleep(10)
 
 
-def _init_http_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    sa = server.socket.getsockname()
-    print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
-def _init_https_endpoints(port, handler_class, server_class=HTTPServer):
-    server = server_class(('', port), handler_class)
-    server.socket = ssl.wrap_socket(server.socket,
-                                    keyfile="certs/server.key", certfile="certs/server.crt",
-                                    ca_certs="certs/client.crt", server_side=True)
-    sa = server.socket.getsockname()
-    print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
-    server.serve_forever()
-
-
 if __name__ == '__main__':
     _main_()
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP_simulator b/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP_simulator
index 40e1af0..8139fc3 100644
--- a/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP_simulator
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/DMaaP_simulator
@@ -1,6 +1,6 @@
 FROM python:3-alpine
 
-ADD DMaaP.py /
+ADD DMaaP.py httpServerLib.py /
 COPY certs/* /certs/
 
 CMD [ "python", "./DMaaP.py" ]
diff --git a/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py b/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py
new file mode 100644
index 0000000..dcfdb7c
--- /dev/null
+++ b/tests/dcaegen2/prh-testcases/resources/simulator/httpServerLib.py
@@ -0,0 +1,34 @@
+import _thread
+import ssl
+from http.server import HTTPServer
+
+
+def header_200_and_json(self):
+    self.send_response(200)
+    self.send_header('Content-Type', 'application/json')
+    self.end_headers()
+
+
+def start_http_endpoint(port, handler_class):
+    _thread.start_new_thread(init_http_endpoints, (port, handler_class))
+
+
+def start_https_endpoint(port, handler_class, keyfile="certs/server.key",
+                         certfile="certs/server.crt", ca_certs="certs/client.crt"):
+    _thread.start_new_thread(init_https_endpoints, (port, handler_class, keyfile, certfile, ca_certs))
+
+
+def init_http_endpoints(port, handler_class, server_class=HTTPServer):
+    server = server_class(('', port), handler_class)
+    sa = server.socket.getsockname()
+    print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
+    server.serve_forever()
+
+
+def init_https_endpoints(port, handler_class, keyfile, certfile, ca_certs, server_class=HTTPServer):
+    server = server_class(('', port), handler_class)
+    server.socket = ssl.wrap_socket(server.socket, keyfile=keyfile, certfile=certfile,
+                                    ca_certs=ca_certs, server_side=True)
+    sa = server.socket.getsockname()
+    print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
+    server.serve_forever()