import base64
 import json
-import requests
+import python_moonutilities.request_wrapper as requests
 import logging.config
 from python_moonutilities import exceptions
 
     config = get_configuration("logging")
     logging.config.dictConfig(config['logging'])
 
-
 def increment_port():
-    components_port_start = int(get_configuration("components_port_start")['components_port_start'])
-    components_port_start += 1
-    url = "http://{}:{}/v1/kv/components_port_start".format(CONSUL_HOST, CONSUL_PORT)
+    components_object = get_configuration("components/port_start")
+    if 'port_start' in components_object:
+        components_port_start = int(get_configuration("components/port_start")['port_start'])
+        components_port_start += 1
+    else:
+        raise exceptions.ConsulComponentContentError("error={}".format(components_object))
+    url = "http://{}:{}/v1/kv/components/port_start".format(CONSUL_HOST, CONSUL_PORT)
     req = requests.put(url, json=str(components_port_start))
     if req.status_code != 200:
         logger.info("url={}".format(url))
         raise exceptions.ConsulError
     return components_port_start
 
-
 def get_configuration(key):
     url = "http://{}:{}/v1/kv/{}".format(CONSUL_HOST, CONSUL_PORT, key)
     req = requests.get(url)
     data = req.json()
     if len(data) == 1:
         data = data[0]
-        return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+        if all( k in data for k in ("Key", "Value")) :
+            return {data["Key"]: json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+        raise exceptions.ConsulComponentContentError("error={}".format(data))
     else:
         return [
-            {item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8"))}
-            for item in data
+            {
+                item["Key"]: json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
+                if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item))
+            } for item in data
         ]
 
-
 def add_component(name, uuid, port=None, bind="127.0.0.1", keystone_id="", extra=None, container=None):
     data = {
         "hostname": name,
     logger.info("Add component {}".format(req.text))
     return configuration.get_configuration("components/"+uuid)
 
-
 def get_plugins():
     url = "http://{}:{}/v1/kv/plugins?recurse=true".format(CONSUL_HOST, CONSUL_PORT)
     req = requests.get(url)
     data = req.json()
     if len(data) == 1:
         data = data[0]
-        return {data["Key"].replace("plugins/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+        if all(k in data for k in ("Key", "Value")):
+            return {data["Key"].replace("plugins/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+        raise exceptions.ConsulComponentContentError("error={}".format(data))
     else:
         return {
             item["Key"].replace("plugins/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
+            if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item))
             for item in data
         }
 
-
 def get_components():
     url = "http://{}:{}/v1/kv/components?recurse=true".format(CONSUL_HOST, CONSUL_PORT)
     req = requests.get(url)
     data = req.json()
     if len(data) == 1:
         data = data[0]
-        return {data["Key"].replace("components/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+        if all(k in data for k in ("Key", "Value")):
+            return {data["Key"].replace("components/", ""): json.loads(base64.b64decode(data["Value"]).decode("utf-8"))}
+        raise exceptions.ConsulComponentContentError("error={}".format(data))
     else:
         return {
             item["Key"].replace("components/", ""): json.loads(base64.b64decode(item["Value"]).decode("utf-8"))
+            if all(k in item for k in ("Key", "Value")) else logger.warning("invalidate content {}".format(item))
             for item in data
         }
 
 
             json=[{'Key': component, 'Value': comp_util.get_b64_conf(component)}]
         )
     m.register_uri(
-        'GET', 'http://consul:8500/v1/kv/components_port_start',
-        json=[{'Key': 'components_port_start', 'Value': comp_util.get_b64_conf("components/port_start")}]
+        'GET', 'http://consul:8500/v1/kv/components/port_start',
+        json=[{'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}]
     )
     m.register_uri(
-        'PUT', 'http://consul:8500/v1/kv/components_port_start',
+        'PUT', 'http://consul:8500/v1/kv/components/port_start',
         json=[]
     )
 
 
 
 def test_get_configuration_success():
     from python_moonutilities import configuration
-    assert configuration.get_configuration("components/port_start")["components/port_start"] == comp_util.CONF["components"]["port_start"]
+    assert configuration.get_configuration("components/port_start")["port_start"] == comp_util.CONF["components"]["port_start"]
 
 
 @requests_mock.Mocker(kw='mock')
         configuration.get_configuration("components/port_start_wrong")
     assert str(exception_info.value) == '500: Consul error'
 
-
-# [TODO] this test used to test the invalid response
-# it should be un commented and run after refactoring the related part
 @requests_mock.Mocker(kw='mock')
 def test_get_configuration_invalid_response(**kwargs):
     from python_moonutilities import configuration
 
-    kwargs['mock'].get('http://consul:8500/v1/kv/components_port_start', json=[
-        {"components_port_start":'components_port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
+    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[
+        {"port_start":'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
     ])
-    # with pytest.raises(Exception) as exception_info:
-    # configuration.get_configuration("components_port_start")
-    # assert str(exception_info.value) == '500: Consul error'
+    with pytest.raises(Exception) as exception_info:
+        configuration.get_configuration("components/port_start")
+    assert str(exception_info.value) == '500: Consul error'
 
 
 @requests_mock.Mocker(kw='mock')
 def test_put_increment_port_failure(**kwargs):
     from python_moonutilities import configuration
-    kwargs['mock'].put('http://consul:8500/v1/kv/components_port_start', json=[], status_code=400)
-    kwargs['mock'].get('http://consul:8500/v1/kv/components_port_start', json=[
-        {'Key': 'components_port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
+    kwargs['mock'].put('http://consul:8500/v1/kv/components/port_start', json=[], status_code=400)
+    kwargs['mock'].get('http://consul:8500/v1/kv/components/port_start', json=[
+        {'Key': 'port_start', 'Value': comp_util.get_b64_conf("components/port_start")}
     ], status_code=200)
     with pytest.raises(Exception) as exception_info:
         configuration.increment_port()