Unverified Commit 12458576 authored by Dan Jones's avatar Dan Jones
Browse files

refactor: count api calls to /token on retries

parent 26a724dc
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
"axios": "^1.2.3", "axios": "^1.2.3",
"axios-mock-adapter": "^1.21.2", "axios-mock-adapter": "^1.21.2",
"babel-jest": "^27.4.4", "babel-jest": "^27.4.4",
"backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691a", "backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#1fd8622a",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"eslint": "^8.4.1", "eslint": "^8.4.1",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
......
...@@ -87,6 +87,7 @@ class TrackedGenericProtocol extends GenericProtocol { ...@@ -87,6 +87,7 @@ class TrackedGenericProtocol extends GenericProtocol {
} }
Before(function() { Before(function() {
this.api = mockValidConfig.api;
this.schema = mockSchema; this.schema = mockSchema;
this.tracker = tracker; this.tracker = tracker;
this.recorder = recorder; this.recorder = recorder;
...@@ -98,11 +99,13 @@ Before(function() { ...@@ -98,11 +99,13 @@ Before(function() {
TrackedAdapter, TrackedAdapter,
TrackedGenericProtocol, TrackedGenericProtocol,
}; };
this.callCounts = {};
this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services); this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services);
this.protocol.setupCallTracking(this.recorder, this.tracker); this.protocol.setupCallTracking(this.recorder, this.tracker);
this.protocol.resetTracker(); this.protocol.resetTracker();
this.mockAxios = mockAxios; this.mockAxios = mockAxios;
this.mockAxios.reset(); this.mockAxios.reset();
this.mockAxios.resetHistory();
this.mockAxios.onGet( this.mockAxios.onGet(
`${mockValidConfig.api}/token`, `${mockValidConfig.api}/token`,
......
...@@ -12,10 +12,14 @@ When('a mock notify API response is configured to return success', function() { ...@@ -12,10 +12,14 @@ When('a mock notify API response is configured to return success', function() {
).reply(200, response); ).reply(200, response);
}); });
When('a mock notify API response is configured to return an error', function() { When('a mock notify API response is configured to return a {int} error', function(statusCode) {
const statusMessages = {
403: 'Token expired',
503: 'Service unavailable'
};
this.mockAxios.onPost( this.mockAxios.onPost(
`${mockValidConfig.api}/notify`, `${mockValidConfig.api}/notify`,
).reply(403, { message: 'Token expired' }) ).reply(statusCode, { message: statusMessages[statusCode] })
}); });
When('the broadcast method is called', function() { When('the broadcast method is called', function() {
...@@ -23,7 +27,7 @@ When('the broadcast method is called', function() { ...@@ -23,7 +27,7 @@ When('the broadcast method is called', function() {
this.message = message; this.message = message;
const body = JSON.stringify(message); const body = JSON.stringify(message);
this.call = this.adapter.broadcast(body); this.call = this.adapter.broadcast(body);
this.broadcastCallCount = this.adapter.getTrackedCalls('broadcast').length; this.callCounts.broadcast = this.adapter.getTrackedCalls('broadcast').length;
}); });
When('the broadcast method is called with is_retry on', function() { When('the broadcast method is called with is_retry on', function() {
...@@ -31,7 +35,7 @@ When('the broadcast method is called with is_retry on', function() { ...@@ -31,7 +35,7 @@ When('the broadcast method is called with is_retry on', function() {
this.message = message; this.message = message;
const body = JSON.stringify(message); const body = JSON.stringify(message);
this.call = this.adapter.broadcast(body, true); this.call = this.adapter.broadcast(body, true);
this.broadcastCallCount = this.adapter.getTrackedCalls('broadcast').length; this.callCounts.broadcast = this.adapter.getTrackedCalls('broadcast').length;
}); });
Then('the broadcast method was called with is_retry on', function() { Then('the broadcast method was called with is_retry on', function() {
...@@ -42,5 +46,5 @@ Then('the broadcast method was called with is_retry on', function() { ...@@ -42,5 +46,5 @@ Then('the broadcast method was called with is_retry on', function() {
Then('the broadcast method is not called again', function() { Then('the broadcast method is not called again', function() {
let newBroadcastCallCount = this.adapter.getTrackedCalls('broadcast').length; let newBroadcastCallCount = this.adapter.getTrackedCalls('broadcast').length;
assert.equal(this.broadcastCallCount, newBroadcastCallCount); assert.equal(this.callCounts.broadcast, newBroadcastCallCount);
}); });
\ No newline at end of file
...@@ -38,4 +38,30 @@ Then('an error response is returned with status {int}', function(expectedStatus) ...@@ -38,4 +38,30 @@ Then('an error response is returned with status {int}', function(expectedStatus)
Then('the credentials are deleted', function() { Then('the credentials are deleted', function() {
assert.equal(this.adapter.credentials, null); assert.equal(this.adapter.credentials, null);
}); });
\ No newline at end of file
Then('the credentials are not deleted', function() {
assert.notEqual(this.adapter.credentials, null);
});
When('the {string} method call counts are checked', function(method) {
let newCallCount = this.adapter.getTrackedCalls(method).length;
this.callCounts[method] = newCallCount;
});
Then('the {string} method is not called again', function(method) {
let newCallCount = this.adapter.getTrackedCalls(method).length;
assert.equal(this.callCounts[method], newCallCount);
});
Then('the total number of calls to {string} was {int}', function(method, expectedCallCount) {
let callCount = this.adapter.getTrackedCalls(method).length;
assert.equal(callCount, expectedCallCount);
});
Then('the total number of {string} requests to {string} was {int}', function(method, endpoint, expectedCallCount) {
let url = `${this.api}${endpoint}`;
let requestHistory = this.mockAxios.history[method.toLowerCase()].filter((request) => request.url === url);
assert.equal(requestHistory.length, expectedCallCount);
});
...@@ -2,7 +2,9 @@ const assert = require('assert'); ...@@ -2,7 +2,9 @@ const assert = require('assert');
const { When, Then } = require('@cucumber/cucumber'); const { When, Then } = require('@cucumber/cucumber');
When('the getAuthorizationHeader method is called', function() { When('the getAuthorizationHeader method is called', function() {
this.call = this.adapter.getAuthorizationHeader() this.call = this.adapter.getAuthorizationHeader();
let callCount = this.adapter.getTrackedCalls('getAuthorizationHeader').length;
this.callCounts.getAuthorizationHeader = callCount;
}); });
Then('a headers object is returned containing a bearer token authorization header', function() { Then('a headers object is returned containing a bearer token authorization header', function() {
...@@ -12,4 +14,4 @@ Then('a headers object is returned containing a bearer token authorization heade ...@@ -12,4 +14,4 @@ Then('a headers object is returned containing a bearer token authorization heade
assert.ok(authHeaderWords[0] === 'Bearer'); assert.ok(authHeaderWords[0] === 'Bearer');
assert.ok(authHeaderWords[1] === this.adapter.credentials.token); assert.ok(authHeaderWords[1] === this.adapter.credentials.token);
}); });
}); });
\ No newline at end of file
...@@ -24,14 +24,19 @@ When('a mock receive API response is configured to return {int} messages', funct ...@@ -24,14 +24,19 @@ When('a mock receive API response is configured to return {int} messages', funct
).reply(200, response); ).reply(200, response);
}); });
When('a mock receive API response is configured to return an error', function() { When('a mock receive API response is configured to return a {int} error', function(statusCode) {
const statusMessages = {
403: 'Token expired',
503: 'Service unavailable'
};
this.mockAxios.onGet( this.mockAxios.onGet(
`${mockValidConfig.api}/receive`, `${mockValidConfig.api}/receive`,
).reply(403, { message: 'Token expired' }) ).reply(statusCode, { message: statusMessages[statusCode] })
}); });
When('the poll method is called', function() { When('the poll method is called', function() {
this.call = this.adapter.poll(); this.call = this.adapter.poll();
this.callCounts.poll = this.adapter.getTrackedCalls('poll').length;
}); });
Then('a successful response is returned with {int} messages', function(xMessages) { Then('a successful response is returned with {int} messages', function(xMessages) {
...@@ -48,7 +53,7 @@ Then('the protocol {string} method is called {int} times', function(method, xInv ...@@ -48,7 +53,7 @@ Then('the protocol {string} method is called {int} times', function(method, xInv
When('the poll method is called with is_retry on', function() { When('the poll method is called with is_retry on', function() {
this.call = this.adapter.poll(true); this.call = this.adapter.poll(true);
this.pollCallCount = this.adapter.getTrackedCalls('poll').length; this.callCounts.poll = this.adapter.getTrackedCalls('poll').length;
}); });
Then('the poll method was called with is_retry on', function() { Then('the poll method was called with is_retry on', function() {
...@@ -59,5 +64,5 @@ Then('the poll method was called with is_retry on', function() { ...@@ -59,5 +64,5 @@ Then('the poll method was called with is_retry on', function() {
Then('the poll method is not called again', function() { Then('the poll method is not called again', function() {
let newPollCallCount = this.adapter.getTrackedCalls('poll').length; let newPollCallCount = this.adapter.getTrackedCalls('poll').length;
assert.equal(this.pollCallCount, newPollCallCount); assert.equal(this.callCounts.poll, newPollCallCount);
}); });
...@@ -12,10 +12,14 @@ When('a mock send API response is configured to return success', function() { ...@@ -12,10 +12,14 @@ When('a mock send API response is configured to return success', function() {
).reply(200, response); ).reply(200, response);
}); });
When('a mock send API response is configured to return an error', function() { When('a mock send API response is configured to return a {int} error', function(statusCode) {
const statusMessages = {
403: 'Token expired',
503: 'Service unavailable'
};
this.mockAxios.onPost( this.mockAxios.onPost(
`${mockValidConfig.api}/send`, `${mockValidConfig.api}/send`,
).reply(403, { message: 'Token expired' }) ).reply(statusCode, { message: statusMessages[statusCode] })
}); });
When('the publish method is called', function() { When('the publish method is called', function() {
...@@ -24,7 +28,7 @@ When('the publish method is called', function() { ...@@ -24,7 +28,7 @@ When('the publish method is called', function() {
const topic = message.metadata.destination; const topic = message.metadata.destination;
const body = JSON.stringify(message); const body = JSON.stringify(message);
this.call = this.adapter.publish(topic, body); this.call = this.adapter.publish(topic, body);
this.publishCallCount = this.adapter.getTrackedCalls('publish').length; this.callCounts.publish = this.adapter.getTrackedCalls('publish').length;
}); });
When('the publish method is called with is_retry on', function() { When('the publish method is called with is_retry on', function() {
...@@ -33,7 +37,7 @@ When('the publish method is called with is_retry on', function() { ...@@ -33,7 +37,7 @@ When('the publish method is called with is_retry on', function() {
const topic = message.metadata.destination; const topic = message.metadata.destination;
const body = JSON.stringify(message); const body = JSON.stringify(message);
this.call = this.adapter.publish(topic, body, true); this.call = this.adapter.publish(topic, body, true);
this.publishCallCount = this.adapter.getTrackedCalls('publish').length; this.callCounts.publish = this.adapter.getTrackedCalls('publish').length;
}); });
Then('the publish method was called with is_retry on', function() { Then('the publish method was called with is_retry on', function() {
...@@ -44,5 +48,5 @@ Then('the publish method was called with is_retry on', function() { ...@@ -44,5 +48,5 @@ Then('the publish method was called with is_retry on', function() {
Then('the publish method is not called again', function() { Then('the publish method is not called again', function() {
let newPublishCallCount = this.adapter.getTrackedCalls('publish').length; let newPublishCallCount = this.adapter.getTrackedCalls('publish').length;
assert.equal(this.publishCallCount, newPublishCallCount); assert.equal(this.callCounts.publish, newPublishCallCount);
}); });
\ No newline at end of file
...@@ -1318,9 +1318,9 @@ babel-preset-jest@^27.5.1: ...@@ -1318,9 +1318,9 @@ babel-preset-jest@^27.5.1:
babel-plugin-jest-hoist "^27.5.1" babel-plugin-jest-hoist "^27.5.1"
babel-preset-current-node-syntax "^1.0.0" babel-preset-current-node-syntax "^1.0.0"
"backbone-adapter-testsuite@git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691a": "backbone-adapter-testsuite@git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#1fd8622a":
version "0.0.1" version "0.0.1"
resolved "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691affa53b04ac65b5fe7dc90df8e0286867" resolved "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#1fd8622a217eda84f05c91a9a7181d5cfad0aace"
balanced-match@^1.0.0: balanced-match@^1.0.0:
version "1.0.2" version "1.0.2"
......
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