Unverified Commit 1ff6f48a authored by Dan Jones's avatar Dan Jones
Browse files

test: test validate method

Add test to check that the mock schema is valid
If the mock schema is not valid then invalid messages
can be marked as valid which makes the tests ineffective
parent 8b0e7ce8
......@@ -27,7 +27,10 @@ class Adapter {
validate(message) {
return this.validator.validate(
message,
this.protocol.schema.definitions.Message
this.protocol.schema.components.schemas.Message,
this.protocol.schema.components.schemas,
false,
true
);
}
......
......@@ -29,7 +29,10 @@ class Adapter {
validate(message) {
return this.validator.validate(
message,
this.protocol.schema.definitions.Message
this.protocol.schema.components.schemas.Message,
this.protocol.schema.components.schemas,
false,
true
);
}
......
......@@ -35,7 +35,13 @@ class GenericProtocol {
* @returns {object}
*/
validate(message) {
return this.validator.validate(message, this.schema.definitions.Message);
return this.validator.validate(
message,
this.protocol.schema.components.schemas.Message,
this.protocol.schema.components.schemas,
false,
true
);
}
/**
......
......@@ -37,7 +37,13 @@ class GenericProtocol {
* @returns {object}
*/
validate(message) {
return this.validator.validate(message, this.schema.definitions.Message);
return this.validator.validate(
message,
this.protocol.schema.components.schemas.Message,
this.protocol.schema.components.schemas,
false,
true
);
}
/**
......
......@@ -9,13 +9,21 @@ const mockAxios = new MockAdapter(axios);
const { fixtures } = require('../../test/fixtures/server');
const mockValidConfig = fixtures.get('valid-config');
const mockInvalidConfig = fixtures.get('invalid-config');
const mockValidConfig = fixtures.get('config-valid');
const mockInvalidConfig = fixtures.get('config-invalid');
const mockSchema = require('../../test/mock/swagger.json');
const { Adapter } = require('../../dist/adapter');
const { GenericProtocol } = require('../../dist/protocol');
let decodeTracker;
class TrackedGenericProtocol extends GenericProtocol {
decode() {
return super.decode()
}
}
Before(function() {
this.mockAxios = mockAxios;
this.mockAxios.reset();
......
......@@ -3,8 +3,7 @@ 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 mockValidConfig = fixtures.get('config-valid');
const xMessageResponse = function(xMessages) {
const message = fixtures.get('message-vehicle-status');
......
const assert = require('assert');
const { Before, Given, When, Then } = require('@cucumber/cucumber');
const { fixtures } = require('../../test/fixtures/server');
Given('a valid message', function() {
this.message = fixtures.get('message-vehicle-status');
});
Given('an invalid message', function() {
this.message = fixtures.get('message-vehicle-status-invalid');
});
When('the validate method is called', function() {
this.validation = this.adapter.validate(this.message);
});
Then('the message is validated successfully', function() {
assert.equal(this.validation.valid, true);
assert.equal(this.validation.errorCount, 0);
});
Then('the message fails to validate', function() {
assert.equal(this.validation.valid, false);
assert.notEqual(this.validation.errorCount, 0);
});
\ No newline at end of file
Feature: Can the adapter validate messages?
The adapter validate method works as expected
Scenario: A valid message is successfully validated against the protocol schema
Given valid config
Given a valid message
When the adapter instance is created
When the validate method is called
Then the message is validated successfully
Scenario: An invalid message fails to validate against the protocol schema
Given valid config
Given an invalid message
When the adapter instance is created
When the validate method is called
Then the message fails to validate
\ No newline at end of file
const assert = require('assert');
const { Given, When, Then } = require('@cucumber/cucumber');
const OpenAPISchemaValidator = require('openapi-schema-validator').default;
const fs = require('fs');
const schemaLocation = './test/mock/swagger.json';
Given('the test schema', function() {
this.schema = JSON.parse(fs.readFileSync(schemaLocation));
});
When('it is validated', function() {
const validator = new OpenAPISchemaValidator({ version: 3 });
this.validation = validator.validate(this.schema);
});
Then('it matches the OpenAPI specification', function() {
// According to the docs this should return a valid:boolean
// but if you look at the code it just returns a list of errors
// which is empty for a valid result
assert.equal(this.validation.errors.length, 0);
});
\ No newline at end of file
# If the mock schema fixture fails to validate
# it can cause invalid messages to show as valid
Feature: Is the mock schema valid?
The mock schema must validate in order for the adapter test to work
Scenario: The schema matches the OpenAPI specification
Given the test schema
When it is validated
Then it matches the OpenAPI specification
\ No newline at end of file
......@@ -51,6 +51,7 @@
"husky": "^7.0.4",
"jest": "^27.4.4",
"lint-staged": "^12.1.2",
"openapi-schema-validator": "^12.1.0",
"prettier": "^2.5.1",
"rollup": "^3.9.1"
}
......
......@@ -27,7 +27,10 @@ export class Adapter {
validate(message) {
return this.validator.validate(
message,
this.protocol.schema.definitions.Message
this.protocol.schema.components.schemas.Message,
this.protocol.schema.components.schemas,
false,
true
);
}
......
......@@ -35,7 +35,13 @@ export class GenericProtocol {
* @returns {object}
*/
validate(message) {
return this.validator.validate(message, this.schema.definitions.Message);
return this.validator.validate(
message,
this.protocol.schema.components.schemas.Message,
this.protocol.schema.components.schemas,
false,
true
);
}
/**
......
{
"metadata": {
"source": "ae",
"destination": "soar.po.ecosub.eco1",
"delivery_type": "publish",
"message_id": "test"
},
"payload": {
"message_type": "VehicleStatus",
"xoperator_id": 1,
"xvehicle_id": 12,
"coordinates": {
"latitude": "monkeys",
"longitude": "janvier",
"depth": "twenty five metres please",
"projection": 4326
},
"battery_percentage": "plenty"
}
}
\ No newline at end of file
{
"message_type": "VehicleStatus",
"headers": {
"metadata": {
"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
"payload": {
"message_type": "VehicleStatus",
"operator_id": "po",
"vehicle_id": "eco1",
"coordinates": {
"latitude": 57.234,
"longitude": -8.432,
"depth": 50,
"projection": 4326
},
"battery_percentage": 64
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ exports.fixtures = {
let fixture = JSON.parse(fixtureContent);
return fixture;
} catch(e) {
console.error('Fixture not found', fixrureName);
console.error('Fixture not found', fixtureName);
return null;
}
}
......
{
"message_type": "VehicleMission",
"headers": {
"metadata": {
"source": "ae",
"destination": "soar.po.ecosub.eco1",
"delivery_type": "publish",
......@@ -12,6 +12,6 @@
"latitude": 59.234,
"longitude": -10.432,
"depth": 50,
"projection": "EPSG:4326"
"projection": 4326
}
}
{
"message_type": "VehicleMission",
"headers": {
"metadata": {
"source": "ae",
"destination": "soar.po.ecosub.eco1",
"delivery_type": "publish",
......@@ -12,7 +12,7 @@
"latitude": 59.234,
"longitude": -10.432,
"depth": 50,
"projection": "EPSG:4326"
"projection": 4326
},
"actions": [
{
......@@ -21,7 +21,7 @@
"latitude": 59.234,
"longitude": -10.432,
"depth": 50,
"projection": "EPSG:4326"
"projection": 4326
}
},
{
......@@ -30,7 +30,7 @@
"latitude": 59.234,
"longitude": -10.432,
"altitude": 50,
"projection": "EPSG:4326"
"projection": 4326
}
}
]
......
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