From bb722d6ba2520468092d090ddc407a9945e717a2 Mon Sep 17 00:00:00 2001
From: Dan Jones <dan.jones@noc.ac.uk>
Date: Tue, 17 Jan 2023 12:59:51 +0000
Subject: [PATCH] test: add tests for adapter broadcast method

---
 features/adapter_broadcast.feature       | 18 ++++++++++++++++
 test/cucumber/adapter/before.steps.js    |  4 ++--
 test/cucumber/adapter/broadcast.steps.js | 26 ++++++++++++++++++++++++
 3 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 features/adapter_broadcast.feature
 create mode 100644 test/cucumber/adapter/broadcast.steps.js

diff --git a/features/adapter_broadcast.feature b/features/adapter_broadcast.feature
new file mode 100644
index 0000000..e37344a
--- /dev/null
+++ b/features/adapter_broadcast.feature
@@ -0,0 +1,18 @@
+Feature: Can the adapter broadcast messages?
+  The adapter publish method works as expected
+
+  Scenario: A message can be published successfully
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When a mock notify API response is configured to return success
+    When the broadcast method is called
+    Then a successful response is returned with status 200
+
+  Scenario: A failed publish returns a 403
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When a mock notify API response is configured to return an error
+    When the broadcast method is called
+    Then an error response is returned with status 403
\ No newline at end of file
diff --git a/test/cucumber/adapter/before.steps.js b/test/cucumber/adapter/before.steps.js
index 9471285..90505ec 100644
--- a/test/cucumber/adapter/before.steps.js
+++ b/test/cucumber/adapter/before.steps.js
@@ -60,8 +60,8 @@ Before(function() {
   this.tracker = tracker;
   this.recorder = recorder;
   let services = {
-    recorder: this.recorder,
-    tracker: this.tracker
+    recorder,
+    tracker
   };
   this.protocol = new TrackedGenericProtocol(this.schema, services);
   this.protocol.resetTracker();
diff --git a/test/cucumber/adapter/broadcast.steps.js b/test/cucumber/adapter/broadcast.steps.js
new file mode 100644
index 0000000..38e495d
--- /dev/null
+++ b/test/cucumber/adapter/broadcast.steps.js
@@ -0,0 +1,26 @@
+const assert = require('assert');
+const { When, Then } = require('@cucumber/cucumber');
+
+const { fixtures } = require('../../fixtures/server');
+
+const mockValidConfig = fixtures.get('config-valid');
+
+When('a mock notify API response is configured to return success', function() {
+  const response = {};
+  this.mockAxios.onPost(
+    `${mockValidConfig.api}/notify`, 
+  ).reply(200, response);
+});
+
+When('a mock notify API response is configured to return an error', function() {
+  this.mockAxios.onPost(
+    `${mockValidConfig.api}/notify`,
+  ).reply(403, { message: 'Token expired' })
+});
+
+When('the broadcast method is called', function() {
+  const message = fixtures.get('message-vehicle-status');
+  this.message = message;
+  const body = JSON.stringify(message);
+  this.call = this.adapter.broadcast(body);
+});
\ No newline at end of file
-- 
GitLab