From 1c60b87da5e3c35e029ffddb72152f0d71718fb6 Mon Sep 17 00:00:00 2001
From: Wang Qi <wangq.fnst@cn.fujitsu.com>
Date: Fri, 10 Sep 2021 10:11:46 +0800
Subject: Add testcase of agl-service-navigation

Bug-AGL: SPEC-4068

Signed-off-by: Wang Qi <wangq.fnst@cn.fujitsu.com>
Change-Id: I34f1730427b3823a69041a4298700221b2adbd70
---
 pyagl/pytest.ini               |  1 +
 pyagl/services/navigation.py   | 54 ++++++++++++++++++++++++++++++++
 pyagl/tests/test_navigation.py | 71 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 pyagl/services/navigation.py
 create mode 100644 pyagl/tests/test_navigation.py

diff --git a/pyagl/pytest.ini b/pyagl/pytest.ini
index 009b05e..6fec1e8 100644
--- a/pyagl/pytest.ini
+++ b/pyagl/pytest.ini
@@ -23,3 +23,4 @@ markers =
   hvac: agl-service-hvac tests
   taskmanager: agl-service-taskmanager tests
   platforminfo: agl-service-platform-info tests
+  navigation: agl-service-navigation tests
diff --git a/pyagl/services/navigation.py b/pyagl/services/navigation.py
new file mode 100644
index 0000000..66b19b7
--- /dev/null
+++ b/pyagl/services/navigation.py
@@ -0,0 +1,54 @@
+# Copyright (C) 2021 Fujitsu
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from pyagl.services.base import AGLBaseService, AFBResponse
+import asyncio
+import os
+
+class NavigationService(AGLBaseService):
+    service = 'agl-service-navigation'
+    parser = AGLBaseService.getparser()
+
+    def __init__(self, ip, port=None):
+        super().__init__(api='navigation', ip=ip, port=port, service='agl-service-navigation')
+
+    async def subscribe(self, event='status'):
+        return await super().subscribe(event=event)
+
+    async def unsubscribe(self, event='status'):
+        return await super().unsubscribe(event=event)
+
+    async def broadcast_status(self, state):
+        return await self.request('broadcast_status', {'state': state})
+
+    async def broadcast_position(self):
+        return await self.request('broadcast_position')
+
+    async def broadcast_waypoints(self):
+        return await self.request('broadcast_waypoints')
+
+
+async def main(loop):
+    args = NavigationService.parser.parse_args()
+    tps = await NavigationService(ip=args.ipaddr, port=args.port)
+
+    if args.subscribe:
+        for event in args.subscribe:
+            msgid = await tps.subscribe(event)
+            print(f'Subscribed for event {event} with messageid {msgid}')
+            print(await tps.afbresponse())
+
+if __name__ == '__main__':
+    loop = asyncio.get_event_loop()
+    loop.run_until_complete(main(loop))
diff --git a/pyagl/tests/test_navigation.py b/pyagl/tests/test_navigation.py
new file mode 100644
index 0000000..50e4894
--- /dev/null
+++ b/pyagl/tests/test_navigation.py
@@ -0,0 +1,71 @@
+
+# Copyright (C) 2021 Fujitsu
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import asyncio
+import os
+import pytest
+import logging
+from pyagl.services.base import AFBResponse, AFBT
+
+from pyagl.services.navigation import NavigationService as ngs
+pytestmark = [pytest.mark.asyncio, pytest.mark.navigation]
+
+
+@pytest.fixture(scope='module')
+def event_loop():
+    loop = asyncio.get_event_loop()
+    yield loop
+
+
+@pytest.fixture(scope='module')
+async def service():
+    address = os.environ.get('AGL_TGT_IP', 'localhost')
+    port = os.environ.get('AGL_TGT_PORT', None)
+    svc = await ngs(ip=address, port=port)
+    yield svc
+    await svc.websocket.close()
+
+
+async def test_broadcast_status(event_loop, service: ngs):
+    msgid = await service.broadcast_status('stop')
+    resp = await service.afbresponse()
+    assert resp.status == 'success', resp.info
+
+
+async def test_subscribe(event_loop, service: ngs):
+    msgid = await service.subscribe('status')
+    resp = await service.afbresponse()
+    assert resp.status == 'success', resp.info
+    event = await service.afbresponse()
+    assert event.type == AFBT.EVENT
+    assert event.api == f'{service.api}/status'
+
+
+async def test_unsubscribe(event_loop, service: ngs):
+    msgid = await service.unsubscribe('status')
+    resp = await service.afbresponse()
+    assert resp.status == 'success', resp.info
+
+
+async def test_broadcast_position(event_loop, service: ngs):
+    msgid = await service.broadcast_position()
+    resp = await service.afbresponse()
+    assert resp.status == 'success', resp.info
+
+
+async def test_broadcast_waypoints(event_loop, service: ngs):
+    msgid = await service.broadcast_waypoints()
+    resp = await service.afbresponse()
+    assert resp.status == 'success', resp.info
-- 
cgit