Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions shared/src/document-loader/calmhub-document-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import axios from 'axios';
import AxiosMockAdapter from 'axios-mock-adapter';
import { CalmHubDocumentLoader } from './calmhub-document-loader';
import { SchemaDirectory } from '../schema-directory';

const calmHubBaseUrl = 'http://local-calmhub';

const ax = axios.create({ baseURL: calmHubBaseUrl });
const mock = new AxiosMockAdapter(ax);

mock.onGet('/calm/schemas/2025-03/meta/core.json').reply(200, {
mock.onGet('/schemas/2025-03/meta/core.json').reply(200, {
'$id': 'https://calm.finos.org/calm/schemas/2025-03/meta/core.json',
'value': 'test'
});

describe('calmhub-document-loader', () => {
let calmHubDocumentLoader;
let calmHubDocumentLoader: CalmHubDocumentLoader;
let schemaDirectory: SchemaDirectory;
beforeEach(() => {
calmHubDocumentLoader = new CalmHubDocumentLoader(calmHubBaseUrl, false, ax);
calmHubDocumentLoader.initialise(schemaDirectory);
});

it('loads a document from CalmHub', async () => {
const calmHubUrl = 'https://calm.finos.org/calm/schemas/2025-03/meta/core.json';
const calmHubUrl = 'calm:/schemas/2025-03/meta/core.json';
const document = await calmHubDocumentLoader.loadMissingDocument(calmHubUrl, 'schema');
expect(document).toEqual({
'$id': 'https://calm.finos.org/calm/schemas/2025-03/meta/core.json',
Expand All @@ -28,7 +31,13 @@ describe('calmhub-document-loader', () => {
});

it('throws an error when the document is not found', async () => {
const calmHubUrl = 'https://calm.finos.org/calm/schemas/2025-03/meta/nonexistent.json';
const calmHubUrl = 'calm:/schemas/2025-03/meta/nonexistent.json';

await expect(calmHubDocumentLoader.loadMissingDocument(calmHubUrl, 'schema')).rejects.toThrow();
});

it('throws an error when the protocol is not calm:', async () => {
const calmHubUrl = 'https://not.calmhub.com/schemas/2025-03/meta/nonexistent.json';

await expect(calmHubDocumentLoader.loadMissingDocument(calmHubUrl, 'schema')).rejects.toThrow();
});
Expand Down
6 changes: 5 additions & 1 deletion shared/src/document-loader/calmhub-document-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { Axios } from 'axios';
import { SchemaDirectory } from '../schema-directory';
import { CalmDocumentType, DocumentLoader } from './document-loader';
import { CalmDocumentType, DocumentLoader, CALM_HUB_PROTO } from './document-loader';
import { initLogger, Logger } from '../logger';

export class CalmHubDocumentLoader implements DocumentLoader {
Expand Down Expand Up @@ -47,6 +47,10 @@ export class CalmHubDocumentLoader implements DocumentLoader {

async loadMissingDocument(documentId: string, _: CalmDocumentType): Promise<object> {
const url = new URL(documentId);
const protocol = url.protocol;
if (protocol !== CALM_HUB_PROTO) {
throw new Error(`CalmHubDocumentLoader only loads documents with protocol '${CALM_HUB_PROTO}'. (Requested: ${protocol})`);
}
const path = url.pathname;

this.logger.debug(`Loading CALM schema from ${this.calmHubUrl}${path}`);
Expand Down
10 changes: 4 additions & 6 deletions shared/src/document-loader/document-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,24 @@ describe('DocumentLoader', () => {
vi.resetModules();
});

it('should create a FileSystemDocumentLoader when loadMode is "filesystem"', () => {
it('should create a FileSystemDocumentLoader', () => {

const docLoaderOpts: DocumentLoaderOptions = {
loadMode: 'filesystem',
schemaDirectoryPath: 'schemas'
};

buildDocumentLoader(docLoaderOpts, false);
buildDocumentLoader(docLoaderOpts);

expect(mocks.fsDocLoader).toHaveBeenCalledWith([CALM_META_SCHEMA_DIRECTORY, 'schemas'], false);
});

it('should create a CalmHubDocumentLoader when loadMode is "calmhub"', () => {
it('should create a CalmHubDocumentLoader when calmHubUrl is defined in loader options', () => {

const docLoaderOpts: DocumentLoaderOptions = {
loadMode: 'calmhub',
calmHubUrl: 'https://example.com'
};

buildDocumentLoader(docLoaderOpts, false);
buildDocumentLoader(docLoaderOpts);

expect(mocks.calmHubDocLoader).toHaveBeenCalledWith('https://example.com', false);
});
Expand Down
2 changes: 2 additions & 0 deletions shared/src/document-loader/document-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { MultiStrategyDocumentLoader } from './multi-strategy-document-loader';

export type CalmDocumentType = 'architecture' | 'pattern' | 'schema';

export const CALM_HUB_PROTO = 'calm:';

export interface DocumentLoader {
initialise(schemaDirectory: SchemaDirectory): Promise<void>;
loadMissingDocument(documentId: string, type: CalmDocumentType): Promise<object>;
Expand Down
Loading