Unverified Commit 8b0e7ce8 authored by Dan Jones's avatar Dan Jones
Browse files

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
parent 716ec0de
......@@ -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);
});
}
}
......
......@@ -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);
});
}
}
......
......@@ -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'));
......
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
# 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
......@@ -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": {
......
......@@ -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);
});
}
}
{
"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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment