@case-contract-testing/case
Version:
Next-generation contract testing suite
73 lines (72 loc) • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContractVerifierConnector = void 0;
const ReadingCaseContract_1 = require("../../core/ReadingCaseContract");
const dependencies_1 = require("../dependencies");
const config_1 = require("../../core/config");
const context_1 = require("../../entities/context");
const entities_1 = require("../../entities");
const readContractFromStore = (config, reader) => {
if (config.contractFilename !== undefined &&
typeof config.contractFilename === 'string') {
return [reader.readContract(config.contractFilename)];
}
if (config.contractDir !== undefined &&
typeof config.contractDir === 'string') {
return reader.readContractsFromDir(config.contractDir);
}
throw new entities_1.CaseConfigurationError('No contractFilename or contractDir specified. Must provide one of these so that Case can find the contract(s) to verify');
};
class ContractVerifierConnector {
contracts;
config;
callback;
dependencies;
context;
constructor(userConfig, callback, dependencies = dependencies_1.readerDependencies) {
this.dependencies = dependencies;
this.config = {
...dependencies.defaultConfig,
...userConfig,
};
this.context = // TODO: Extract constructDataContext to somewhere more DRY
(0, context_1.constructDataContext)(this.dependencies.makeLogger, this.dependencies.resultPrinter, {
...(0, config_1.configToRunContext)(this.config),
}, dependencies.defaultConfig);
const store = this.dependencies.makeContractStore(this.context);
this.contracts = readContractFromStore(this.config, store);
this.callback = callback;
}
getAvailableContractDescriptions() {
return this.contracts.map((link) => link.contents.description);
}
verifyContract(invoker, configOverride = {}) {
const mergedConfig = { ...this.config, ...configOverride };
if (typeof mergedConfig.providerName !== 'string') {
throw new entities_1.CaseConfigurationError(`Must provide a providerName to verify (received '${mergedConfig.providerName}').`);
}
this.context.logger.debug(`There are ${this.contracts.length} contracts loaded (this may include contracts that don't belong to this run)`);
this.contracts
.filter((item) => item.contents.description?.providerName !== mergedConfig.providerName)
.forEach((item) => {
this.context.logger.debug(`Skipping ${item.filePath} because it is not for the provider '${mergedConfig.providerName}' (It was for '${item.contents.description?.providerName}' instead)`);
});
const caseContractsForProvider = this.contracts.filter((item) => item.contents.description?.providerName === mergedConfig.providerName);
caseContractsForProvider
.filter((item) => typeof mergedConfig.consumerName !== 'undefined' &&
item.contents.description?.consumerName !== mergedConfig.consumerName)
.forEach((item) => {
this.context.logger.debug(`Skipping ${item.filePath} because it is not for the consumer '${mergedConfig.consumerName}' (It was for '${item.contents.description?.consumerName}' instead)`);
});
const contractsToVerify = caseContractsForProvider.filter((item) => typeof mergedConfig.consumerName === 'undefined' ||
item.contents.description?.consumerName === mergedConfig.consumerName);
if (contractsToVerify.length === 0) {
throw new entities_1.CaseConfigurationError("No contracts were matched for verification. Try this run again with logLevel: 'debug' to find out more");
}
contractsToVerify.map((contractLink) => {
this.context.logger.debug(`Verifying contract from file '${contractLink.filePath}'`);
return new ReadingCaseContract_1.ReadingCaseContract(contractLink.contents, this.dependencies, mergedConfig).verifyContract(invoker, this.callback);
});
}
}
exports.ContractVerifierConnector = ContractVerifierConnector;