diff --git a/package.json b/package.json index 983a81759caed0790eb498cb2d9168892751291b..f8b9ee6fc0c9a826a59f2c1def55c70f73f81243 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "axios": "^1.2.3", "axios-mock-adapter": "^1.21.2", "babel-jest": "^27.4.4", - "backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#2a22ae98", + "backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691a", "cross-env": "^7.0.3", "eslint": "^8.4.1", "eslint-config-prettier": "^8.3.0", diff --git a/test/cucumber/adapter/before.steps.js b/test/cucumber/adapter/before.steps.js index d07405d76ff0d5be1f249e37592df593f43d2c07..fc17ad9d2a9c230fd96280b646e6a6e48cce9247 100644 --- a/test/cucumber/adapter/before.steps.js +++ b/test/cucumber/adapter/before.steps.js @@ -14,6 +14,7 @@ const mockInvalidConfig = fixtures.get('config-invalid'); const mockSchema = fixtures.get('schema-swagger'); const { GenericProtocol } = require('../../../dist/protocol'); +const { Adapter } = require('../../../dist/adapter'); /** * Use assert.CallTracker to track internal method calls @@ -27,11 +28,41 @@ const trackedFunction = function(method, params) { }; const recorder = tracker.calls(trackedFunction); +class TrackedAdapter extends Adapter { + setupCallTracking(recorder, tracker) { + this.recorder = recorder; + this.tracker = tracker; + } + getAuthorizationHeader() { + this.recorder('getAuthorizationHeader'); + return super.getAuthorizationHeader(); + } + poll(is_retry) { + this.recorder('poll', { is_retry }); + return super.poll(is_retry); + } + publish(topic, body, is_retry) { + this.recorder('publish', { topic, body, is_retry }); + return super.publish(topic, body, is_retry); + } + broadcast(body, is_retry) { + this.recorder('broadcast', { body, is_retry }); + return super.broadcast(body, is_retry); + } + getTrackedCalls(method) { + let calls = this.tracker.getCalls(this.recorder); + let methodCalls = calls.filter(call => call.arguments[0] === method); + return methodCalls; + } + resetTracker() { + this.tracker.reset(); + } +} + class TrackedGenericProtocol extends GenericProtocol { - constructor(schema, services) { - super(schema, services); - this.recorder = services.recorder; - this.tracker = services.tracker; + setupCallTracking(recorder, tracker) { + this.recorder = recorder; + this.tracker = tracker; } encode(type, message) { this.recorder('encode', {type, message}); @@ -63,7 +94,12 @@ Before(function() { recorder, tracker }; - this.protocol = new TrackedGenericProtocol(this.schema, services); + this.classes = { + TrackedAdapter, + TrackedGenericProtocol, + }; + this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services); + this.protocol.setupCallTracking(this.recorder, this.tracker); this.protocol.resetTracker(); this.mockAxios = mockAxios; this.mockAxios.reset(); diff --git a/test/cucumber/adapter/broadcast.steps.js b/test/cucumber/adapter/broadcast.steps.js index ad08de51409c35796a905586d260d1d453613013..d0206a7fe25f8b51c6d2ae6004dd0825077eb9a5 100644 --- a/test/cucumber/adapter/broadcast.steps.js +++ b/test/cucumber/adapter/broadcast.steps.js @@ -1,4 +1,5 @@ -const { When } = require('@cucumber/cucumber'); +const assert = require('assert'); +const { When, Then } = require('@cucumber/cucumber'); const { fixtures } = require('../../fixtures/server'); @@ -22,4 +23,24 @@ When('the broadcast method is called', function() { this.message = message; const body = JSON.stringify(message); this.call = this.adapter.broadcast(body); + this.broadcastCallCount = this.adapter.getTrackedCalls('broadcast').length; +}); + +When('the broadcast method is called with is_retry on', function() { + const message = fixtures.get('message-vehicle-status'); + this.message = message; + const body = JSON.stringify(message); + this.call = this.adapter.broadcast(body, true); + this.broadcastCallCount = this.adapter.getTrackedCalls('broadcast').length; +}); + +Then('the broadcast method was called with is_retry on', function() { + let broadcastCalls = this.adapter.getTrackedCalls('broadcast'); + let lastCall = broadcastCalls[broadcastCalls.length-1]; + assert.ok(lastCall.arguments[1].is_retry); +}); + +Then('the broadcast method is not called again', function() { + let newBroadcastCallCount = this.adapter.getTrackedCalls('broadcast').length; + assert.equal(this.broadcastCallCount, newBroadcastCallCount); }); \ No newline at end of file diff --git a/test/cucumber/adapter/common.steps.js b/test/cucumber/adapter/common.steps.js index 3979c109129570ebbebf964d82b37d9529999b96..c48982e1cb5387f3b272353920ffdd6b8170b301 100644 --- a/test/cucumber/adapter/common.steps.js +++ b/test/cucumber/adapter/common.steps.js @@ -6,7 +6,7 @@ const { fixtures } = require('../../fixtures/server'); const mockValidConfig = fixtures.get('config-valid'); const mockInvalidConfig = fixtures.get('config-invalid'); const mockSchema = require('../../mock/swagger.json'); -const { Adapter } = require('../../../dist/adapter'); +// const { Adapter } = require('../../../dist/adapter'); Given('valid config', function() { this.config = mockValidConfig @@ -18,8 +18,8 @@ Given('invalid config', function() { }); When('the adapter instance is created', function() { - let mockAdapter = new Adapter(this.protocol, this.config); - this.adapter = mockAdapter; + this.adapter = new this.classes.TrackedAdapter(this.protocol, this.config); + this.adapter.setupCallTracking(this.recorder, this.tracker); }); Then('a successful response is returned with status {int}', function(expectedStatus) { @@ -34,4 +34,8 @@ Then('an error response is returned with status {int}', function(expectedStatus) .catch((error) => { assert.equal(error.response.status, expectedStatus); }); +}); + +Then('the credentials are deleted', function() { + assert.equal(this.adapter.credentials, null); }); \ No newline at end of file diff --git a/test/cucumber/adapter/poll.steps.js b/test/cucumber/adapter/poll.steps.js index db3b5644d29c19e42cca47e5ff3193c43292b30d..60fc2c03fc495fe9dcbbf710fd1bbe69d61cc9ea 100644 --- a/test/cucumber/adapter/poll.steps.js +++ b/test/cucumber/adapter/poll.steps.js @@ -45,3 +45,19 @@ Then('the protocol {string} method is called {int} times', function(method, xInv const decodes = this.protocol.getTrackedCalls(method); assert.equal(decodes.length, xInvokes); }); + +When('the poll method is called with is_retry on', function() { + this.call = this.adapter.poll(true); + this.pollCallCount = this.adapter.getTrackedCalls('poll').length; +}); + +Then('the poll method was called with is_retry on', function() { + let pollCalls = this.adapter.getTrackedCalls('poll'); + let lastCall = pollCalls[pollCalls.length-1]; + assert.ok(lastCall.arguments[1].is_retry); +}); + +Then('the poll method is not called again', function() { + let newPollCallCount = this.adapter.getTrackedCalls('poll').length; + assert.equal(this.pollCallCount, newPollCallCount); +}); diff --git a/test/cucumber/adapter/publish.steps.js b/test/cucumber/adapter/publish.steps.js index 95a0f6232baac95c29a0a7f6dfc7ee14e446037d..43648c08503ebee1072e2f4200afd70104a66ea4 100644 --- a/test/cucumber/adapter/publish.steps.js +++ b/test/cucumber/adapter/publish.steps.js @@ -24,4 +24,25 @@ When('the publish method is called', function() { const topic = message.metadata.destination; const body = JSON.stringify(message); this.call = this.adapter.publish(topic, body); + this.publishCallCount = this.adapter.getTrackedCalls('publish').length; +}); + +When('the publish method is called with is_retry on', function() { + const message = fixtures.get('message-vehicle-status'); + this.message = message; + const topic = message.metadata.destination; + const body = JSON.stringify(message); + this.call = this.adapter.publish(topic, body, true); + this.publishCallCount = this.adapter.getTrackedCalls('publish').length; +}); + +Then('the publish method was called with is_retry on', function() { + let publishCalls = this.adapter.getTrackedCalls('publish'); + let lastCall = publishCalls[publishCalls.length-1]; + assert.ok(lastCall.arguments[1].is_retry); +}); + +Then('the publish method is not called again', function() { + let newPublishCallCount = this.adapter.getTrackedCalls('publish').length; + assert.equal(this.publishCallCount, newPublishCallCount); }); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 9f040200def8af6bfcfb7f66f09a161894cc27fe..15f970c01e51c2dc031f469197d26700cade779e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1318,9 +1318,9 @@ babel-preset-jest@^27.5.1: babel-plugin-jest-hoist "^27.5.1" babel-preset-current-node-syntax "^1.0.0" -"backbone-adapter-testsuite@git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#2a22ae98": +"backbone-adapter-testsuite@git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691a": version "0.0.1" - resolved "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#2a22ae98e9020a04895f79f6af5fb1c979f62259" + resolved "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dd29691affa53b04ac65b5fe7dc90df8e0286867" balanced-match@^1.0.0: version "1.0.2"