From 8b0e7ce843201c57909b7e5ce257d5a34a2fa9bd Mon Sep 17 00:00:00 2001
From: Dan Jones <dan.jones@noc.ac.uk>
Date: Fri, 13 Jan 2023 14:21:19 +0000
Subject: [PATCH] test: add tests for receive method

+ Ensure dist modules are built before running tests
+ Test successful receive responses
+ TODO test decode and validate are invoked
---
 dist/adapter.esm.js                           | 11 +++++-
 dist/adapter.js                               | 11 +++++-
 .../stepdefs.js => adapter/authenticates.js}  |  8 ++--
 features/adapter/receives.js                  | 37 +++++++++++++++++++
 features/adapter_receives.feature             | 30 +++++++++++++++
 package.json                                  |  2 +-
 src/adapter/index.js                          | 10 +++++
 test/fixtures/message-vehicle-status.json     | 18 +++++++++
 8 files changed, 120 insertions(+), 7 deletions(-)
 rename features/{step_definitions/stepdefs.js => adapter/authenticates.js} (94%)
 create mode 100644 features/adapter/receives.js
 create mode 100644 features/adapter_receives.feature
 create mode 100644 test/fixtures/message-vehicle-status.json

diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js
index 21ae71a..dfd3c83 100644
--- a/dist/adapter.esm.js
+++ b/dist/adapter.esm.js
@@ -10,7 +10,6 @@ class Adapter {
     this.config = config;
     this.axios = axios;
     this.validator = new Validator(protocol.schema);
-    //this.auth();
   }
 
   /**
@@ -109,6 +108,10 @@ class Adapter {
           } 
         });
         return response;
+      })
+      .catch((error) => {
+        console.error(error);
+        return Promise.reject(error.response);
       });
   }
 
@@ -137,6 +140,9 @@ class Adapter {
       })
       .then((response) => {
         return response;
+      })
+      .catch((error) => {
+        return Promise.reject(error.response);
       });
   }
 
@@ -167,6 +173,9 @@ class Adapter {
       })
       .then((response) => {
         return response;
+      })
+      .catch((error) => {
+        return Promise.reject(error.response);
       });
   }
 }
diff --git a/dist/adapter.js b/dist/adapter.js
index fb5072a..5d4ae04 100644
--- a/dist/adapter.js
+++ b/dist/adapter.js
@@ -12,7 +12,6 @@ class Adapter {
     this.config = config;
     this.axios = axios;
     this.validator = new Validator(protocol.schema);
-    //this.auth();
   }
 
   /**
@@ -111,6 +110,10 @@ class Adapter {
           } 
         });
         return response;
+      })
+      .catch((error) => {
+        console.error(error);
+        return Promise.reject(error.response);
       });
   }
 
@@ -139,6 +142,9 @@ class Adapter {
       })
       .then((response) => {
         return response;
+      })
+      .catch((error) => {
+        return Promise.reject(error.response);
       });
   }
 
@@ -169,6 +175,9 @@ class Adapter {
       })
       .then((response) => {
         return response;
+      })
+      .catch((error) => {
+        return Promise.reject(error.response);
       });
   }
 }
diff --git a/features/step_definitions/stepdefs.js b/features/adapter/authenticates.js
similarity index 94%
rename from features/step_definitions/stepdefs.js
rename to features/adapter/authenticates.js
index 926115a..373b9f0 100644
--- a/features/step_definitions/stepdefs.js
+++ b/features/adapter/authenticates.js
@@ -17,15 +17,15 @@ const { Adapter } = require('../../dist/adapter');
 const { GenericProtocol } = require('../../dist/protocol');
 
 Before(function() {
-  
-  mockAxios.reset();
+  this.mockAxios = mockAxios;
+  this.mockAxios.reset();
 
-  mockAxios.onGet(
+  this.mockAxios.onGet(
     `${mockValidConfig.api}/token`, 
     { params: { client_id: mockValidConfig.client_id, secret: mockValidConfig.secret } }
   ).reply(200, fixtures.get('response-valid-token'));
 
-  mockAxios.onGet(
+  this.mockAxios.onGet(
     `${mockInvalidConfig.api}/token`, 
     { params: { client_id: mockInvalidConfig.client_id, secret: mockInvalidConfig.secret } }
   ).reply(403, fixtures.get('response-denied-token'));
diff --git a/features/adapter/receives.js b/features/adapter/receives.js
new file mode 100644
index 0000000..6cbf5bb
--- /dev/null
+++ b/features/adapter/receives.js
@@ -0,0 +1,37 @@
+const assert = require('assert');
+const { Before, Given, When, Then } = require('@cucumber/cucumber');
+
+const { fixtures } = require('../../test/fixtures/server');
+
+const mockValidConfig = fixtures.get('valid-config');
+const mockInvalidConfig = fixtures.get('invalid-config');
+
+const xMessageResponse = function(xMessages) {
+  const message = fixtures.get('message-vehicle-status');
+  let response = [];
+  for (let i=0; i<xMessages; i++) {
+    response.push({
+      topic: "broadcast",
+      message: JSON.stringify(message)
+    });
+  }
+  return response;
+};
+
+When('the queue contains {int} messages', function(xMessages) {
+  const response = xMessageResponse(xMessages);
+  this.mockAxios.onGet(
+    `${mockValidConfig.api}/receive`, 
+  ).reply(200, response);
+});
+
+When('the poll method is called', async function() {
+  this.messages = await this.adapter.poll()
+  .then((response) => {
+    return response.data;
+  });
+});
+
+Then('a successful response is returned with {int} messages', function(xMessages) {
+  assert.equal(this.messages.length, xMessages);
+});
\ No newline at end of file
diff --git a/features/adapter_receives.feature b/features/adapter_receives.feature
new file mode 100644
index 0000000..6582ca3
--- /dev/null
+++ b/features/adapter_receives.feature
@@ -0,0 +1,30 @@
+# When the queue contains x messages 
+# only mocks the API response 
+# Testing how the API behaves with a full queue are defined in the API 
+
+Feature: Can the adapter receive messages?
+  The adapter poll method works as expected
+
+  Scenario: No messages are received succecssfully if the queue is empty
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When the queue contains 0 messages
+    When the poll method is called
+    Then a successful response is returned with 0 messages  
+
+  Scenario: 2 messages are received succecssfully if the queue contains 2 messages
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When the queue contains 2 messages 
+    When the poll method is called
+    Then a successful response is returned with 2 messages
+
+  Scenario: 10 messages are received succecssfully if the queue contains 10 messages
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When the queue contains 10 messages 
+    When the poll method is called
+    Then a successful response is returned with 10 messages
diff --git a/package.json b/package.json
index 4fe1198..c7beded 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,7 @@
     "lint": "yarn lint:js && yarn lint:prettier",
     "lintfix": "prettier --write --list-different . && yarn lint:js --fix",
     "prepare": "husky install",
-    "test": "cucumber-js",
+    "test": "yarn build && yarn cucumber-js",
     "build": "cross-env NODE_ENV=production rollup -c"
   },
   "lint-staged": {
diff --git a/src/adapter/index.js b/src/adapter/index.js
index 8eff44c..2604c5c 100644
--- a/src/adapter/index.js
+++ b/src/adapter/index.js
@@ -108,6 +108,10 @@ export class Adapter {
           } 
         });
         return response;
+      })
+      .catch((error) => {
+        console.error(error);
+        return Promise.reject(error.response);
       });
   }
 
@@ -136,6 +140,9 @@ export class Adapter {
       })
       .then((response) => {
         return response;
+      })
+      .catch((error) => {
+        return Promise.reject(error.response);
       });
   }
 
@@ -166,6 +173,9 @@ export class Adapter {
       })
       .then((response) => {
         return response;
+      })
+      .catch((error) => {
+        return Promise.reject(error.response);
       });
   }
 }
diff --git a/test/fixtures/message-vehicle-status.json b/test/fixtures/message-vehicle-status.json
new file mode 100644
index 0000000..1e47be5
--- /dev/null
+++ b/test/fixtures/message-vehicle-status.json
@@ -0,0 +1,18 @@
+{
+  "message_type": "VehicleStatus",
+  "headers": {
+    "source": "ae",
+    "destination": "soar.po.ecosub.eco1",
+    "delivery_type": "publish",
+    "message_id": "test"
+  },
+  "operator_id": "po",
+  "vehicle_id": "eco1",
+  "coordinates": {
+    "latitude": 57.234,
+    "longitude": -8.432,
+    "depth": 50,
+    "projection": "EPSG:4326"
+  },
+  "battery_percentage": 64
+}
\ No newline at end of file
-- 
GitLab