_send_payload internal def test added with FakeServer
# FOGLAMP_END
from unittest.mock import patch
+from aiohttp import web
+from aiohttp.test_utils import unused_port
import pytest
from foglamp.tasks.north.sending_process import SendingProcess
@@ -17,6 +19,59 @@ __license__ = "Apache 2.0"
__version__ = "${VERSION}"
+_HOST = '127.0.0.1'
+_PORT = unused_port()
+_URL = 'http://{}:{}/ingress/messages'.format(_HOST, _PORT)
+
+
+# TODO: negative tests coverage later on
+# as code needs some refactoring as well on the basis of HTTP status code
+# raise exceptions
+class FakeServer:
+
+ def __init__(self, *, loop):
+ self.loop = loop
+ self.app = web.Application(loop=loop)
+ self.app.router.add_routes([
+ web.post('/ingress/messages', self.send_payload)
+ ])
+ self.handler = None
+ self.server = None
+
+ async def start(self):
+ self.handler = self.app.make_handler()
+ self.server = await self.loop.create_server(self.handler, _HOST, _PORT, ssl=None)
+
+ async def stop(self):
+ self.server.close()
+ await self.server.wait_closed()
+ await self.app.shutdown()
+ await self.handler.shutdown()
+ await self.app.cleanup()
+
+ def send_payload(self, request):
+ body = yield from request.json()
+ return web.json_response(body)
+
+
+@pytest.allure.feature("unit")
+@pytest.allure.story("plugin", "north", "http")
+@pytest.mark.asyncio
+async def test_send_payload(event_loop):
+ fake_server = FakeServer(loop=event_loop)
+ await fake_server.start()
+
+ payloads = [{'id': 1, 'asset_code': 'fogbench/temperature', 'read_key': '31e5ccbb-3e45-4038-95e9-7920834d0852', 'user_ts': '2018-02-26 12:12:54.171949+00', 'reading': {'ambient': 7, 'object': 28}}, {'id': 2, 'asset_code': 'fogbench/luxometer', 'read_key': '9b5beb10-5d87-4cd9-803e-02df7942139d', 'user_ts': '2018-02-27 11:46:57.368753+00', 'reading': {'lux': 92748.668}}]
+ http_north.http_north = HttpNorthPlugin()
+ http_north.http_north.event_loop = event_loop
+ http_north.config = http_north._DEFAULT_CONFIG
+ http_north.config['url']['value'] = _URL
+ last_id, num_count = await http_north.http_north._send_payloads(payloads)
+ assert 2, 2 == (last_id, num_count)
+
+ await fake_server.stop()
+
+
@pytest.allure.feature("unit")
@pytest.allure.story("plugin", "north", "http")
def test_plugin_info():
