wa-chat-server
Version:
Watson Assistant powered chat server
141 lines (140 loc) • 6.72 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaValidatorTest = void 0;
const SchemaValidator_1 = require("../../class/SchemaValidator");
const ConversationServiceMock_1 = require("../mock/ConversationServiceMock");
const ConversationServicesMock_1 = require("../mock/ConversationServicesMock");
const WAChatServerConfigMock_1 = require("../mock/WAChatServerConfigMock");
const SchemaValidationException_1 = require("../../exception/SchemaValidationException");
const Logger_1 = require("../../class/Logger/Logger");
class SchemaValidatorTest {
constructor() {
this.schemaDir = '';
this.config = new WAChatServerConfigMock_1.WAChatServerConfigMock();
this.logger = new Logger_1.Logger(this.config);
}
/**
* An exception is thrown if an obligatory schema is missing
*/
missingSchemasTest() {
it('no-missing-schemas', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const services = new ConversationServicesMock_1.ConversationServicesMock();
const validator = new SchemaValidator_1.SchemaValidator(services);
services.obligatorySchemas = new Set('A');
services.schemaPathToSchema = new Map();
services.schemaPathToSchema.set('A', '{}');
yield validator.validate(); // must be OK
services.schemaPathToSchema.delete('A');
return expect(validator.validate()).rejects.toStrictEqual(new SchemaValidationException_1.SchemaValidationException('Schema A not found!'));
}));
}
/**
* An exception is thrown if there is some schema on the filesystem that is
* not used by any service
*/
extraSchemasTest() {
it('no-extra-schemas', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const services = new ConversationServicesMock_1.ConversationServicesMock();
const validator = new SchemaValidator_1.SchemaValidator(services);
services.schemaPathToSchema = new Map();
services.schemaPathToSchema.set('B', '{}');
services.obligatorySchemas = new Set('B');
yield validator.validate(); // must be OK
services.schemaPathToSchema.set('C', '{}');
services.optionalSchemas = new Set('C');
yield validator.validate(); // must be OK
services.schemaPathToSchema.set('A', '{}');
const errorStr = 'Schema A not used by any conversation service!';
const error = new SchemaValidationException_1.SchemaValidationException(errorStr);
return expect(validator.validate()).rejects.toStrictEqual(error);
}));
}
/**
* An exception is thrown if some of the converation service related schema
* files contains an invalid JSON
*/
invalidJsonTest() {
it('invalid-json', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const services = new ConversationServicesMock_1.ConversationServicesMock();
services.serviceMock = new ConversationServiceMock_1.ConversationServiceMock({
logger: this.logger,
services,
requestValidation: true,
responseValidation: false,
});
const validator = new SchemaValidator_1.SchemaValidator(services);
services.schemaPathToSchema = new Map();
const path = `${this.schemaDir}/request/Mock.json`;
services.schemaPathToSchema.set(path, '{');
services.obligatorySchemas = new Set([path]);
try {
yield validator.validate();
}
catch (e) {
const instanceTest = e instanceof SchemaValidationException_1.SchemaValidationException;
const text = `Error: Schema ${path}: Invalid JSON - `;
const textTest = e.toString().startsWith(text);
expect(instanceTest && textTest).toStrictEqual(true);
}
}));
}
/**
* An exception is thrown if some of the converation service related schema
* files is not a valid JSON schema
*/
invalidSchemaTest() {
it('invalid-schema', () => __awaiter(this, void 0, void 0, function* () {
expect.assertions(1);
const services = new ConversationServicesMock_1.ConversationServicesMock();
services.serviceMock = new ConversationServiceMock_1.ConversationServiceMock({
logger: this.logger,
services,
requestValidation: true,
responseValidation: false,
});
const validator = new SchemaValidator_1.SchemaValidator(services);
services.schemaPathToSchema = new Map();
const path = `${this.schemaDir}/request/Mock.json`;
services.schemaPathToSchema.set(path, JSON.stringify({
type: 'object',
properties: {
date: {
type: 'unexisting-type',
},
},
}));
services.obligatorySchemas = new Set([path]);
try {
yield validator.validate();
}
catch (e) {
const instanceTest = e instanceof SchemaValidationException_1.SchemaValidationException;
const text = `Error: Schema ${path}: Invalid schema - `;
const textTest = e.toString().startsWith(text);
expect(instanceTest && textTest).toStrictEqual(true);
}
}));
}
test() {
return __awaiter(this, void 0, void 0, function* () {
this.missingSchemasTest();
this.extraSchemasTest();
this.invalidJsonTest();
this.invalidSchemaTest();
});
}
}
exports.SchemaValidatorTest = SchemaValidatorTest;
new SchemaValidatorTest().test();