Added helpers to pytest tests
[anna.git] / example / diameter / launcher / resources / rest_api / ct / conftest.py
index fd4f339..81f703b 100644 (file)
@@ -9,6 +9,7 @@ import logging
 import os
 import pytest
 import re
+import xmltodict
 
 #############
 # CONSTANTS #
@@ -128,6 +129,39 @@ def b64_decode():
     return message_bytes.decode('ascii')
   return decode
 
+@pytest.fixture(scope='session')
+def saveB64Artifact(b64_decode):
+  """
+  Decode base64 string provided to disk
+
+  base64_message: base64 encoded string
+  file_basename: file basename where to write
+  isXML: decoded data corresponds to xml string. In this case, also json equivalente representation is written to disk
+  """
+  def save_b64_artifact(base64_message, file_basename, isXML = True):
+
+    targetFile = file_basename + ".txt"
+    data = b64_decode(base64_message)
+
+    if isXML:
+      targetFile = file_basename + ".xml"
+
+    _file = open(targetFile, "w")
+    n = _file.write(data)
+    _file.close()
+
+    if isXML:
+      targetFile = file_basename + ".json"
+      xml_dict = xmltodict.parse(data)
+      data = json.dumps(xml_dict, indent = 4, sort_keys=True)
+
+      _file = open(targetFile, "w")
+      n = _file.write(data)
+      _file.close()
+
+  return save_b64_artifact
+
+
 # HTTP communication:
 class RestClient(object):
     """A client helper to perform rest operations: GET, POST.