diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js index 21ae71a7131ab3865190f32faa788c99be8725c7..dfd3c838ef6639fc9506ebca8b26bf47c2429b6f 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 fb5072a3243af3e41468936bd24b2a1364304384..5d4ae04b5da3f7fc16bed8586da3b545ef6bed5f 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 926115a60c66e6c80d773c1680719a7d906a88c6..373b9f00bd693e55157a259adf427c5e512f685d 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 0000000000000000000000000000000000000000..6cbf5bb0db52d2a54a53fb0c26787efad0df1bf1 --- /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 0000000000000000000000000000000000000000..6582ca348bf392cf0ebd6f636e3564f0f2304ce5 --- /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 4fe1198bdd4b60d55d116e876978633e70f48616..c7bededab3ea6a589efe6cd36ccc050a127ecf6f 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 8eff44cde3c87e2bb77a4357b143a38db52f5a50..2604c5c344d35e01fa0751682f06850e37ac8dd1 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 0000000000000000000000000000000000000000..1e47be57f27c4e72f809a0c2041d75637ca6ecee --- /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