diff --git a/src/compas-services/CompasNSDocFileService.ts b/src/compas-services/CompasNSDocFileService.ts new file mode 100644 index 0000000000..40c096de52 --- /dev/null +++ b/src/compas-services/CompasNSDocFileService.ts @@ -0,0 +1,100 @@ +import { handleError, handleResponse } from './foundation.js'; + +interface NsDocFile { + filename: string; + id: string; + name: string; +} + +// Temporary solution to map to the old logic +const nsDocfiles: NsDocFile[] = [ + { + filename: 'IEC_61850-7-2_2007B3-en.nsdoc', + name: 'IEC 61850-7-2', + id: '87e5bed8-2f27-4006-8673-f9d00b0a5426', + }, + { + filename: 'IEC_61850-7-3_2007B3-en.nsdoc', + name: 'IEC_61850-7-3', + id: '315b02ac-c4aa-4495-9b4f-f7175a75c315', + }, + { + filename: 'IEC_61850-7-4_2007B3-en.nsdoc', + name: 'IEC 61850-7-4', + id: 'da1b2ca0-1263-4b10-9b16-2f148ae3a1f1', + }, + { + filename: 'IEC_61850-8-1_2003A2-en.nsdoc', + name: 'IEC 61850-8-1', + id: '0c052ea7-a010-4ca8-b2c7-caa665cabc46', + }, +]; + +const createElement = ( + name: string, + textContent: string, + document: XMLDocument +): Element => { + const element: Element = document.createElement(name); + element.textContent = textContent; + + return element; +}; + +/** TODO: Make this return JSON */ +export function CompasNSDocFileService() { + return { + listNsdocFiles(): Promise { + const document: XMLDocument = new DOMParser().parseFromString( + '', + 'text/xml' + ); + + nsDocfiles.forEach(nsDocFile => { + const nsDocFileElement: Element = document.createElement('NsdocFile'); + + nsDocFileElement.appendChild( + createElement('Id', nsDocFile.id, document) + ); + nsDocFileElement.appendChild( + createElement('NsdocId', nsDocFile.name, document) + ); + nsDocFileElement.appendChild( + createElement('Checksum', nsDocFile.id, document) + ); + nsDocFileElement.appendChild( + createElement('Filename', nsDocFile.filename, document) + ); + + document + .querySelector('NsdocListResponse')! + .appendChild(nsDocFileElement); + }); + + return Promise.resolve(document); + }, + + getNsdocFile(id: string): Promise { + const nsDocFile: NsDocFile = nsDocfiles.find(f => f.id === id)!; + + if (!nsDocFile) { + return Promise.reject(`Unable to find nsDoc file with id ${id}`); + } + return fetch(`./public/nsdoc/${nsDocFile.filename}`) + .catch(handleError) + .then(handleResponse) + .then(res => { + const document: XMLDocument = new DOMParser().parseFromString( + '', + 'text/xml' + ); + + document + .querySelector('NsdocResponse')! + .appendChild(createElement('NsdocFile', res, document)); + + return document; + }); + }, + }; +} diff --git a/src/compas-services/CompasValidatorService.ts b/src/compas-services/CompasValidatorService.ts index d84b899c1c..07d0212270 100644 --- a/src/compas-services/CompasValidatorService.ts +++ b/src/compas-services/CompasValidatorService.ts @@ -60,14 +60,6 @@ export function CompasSclValidatorService() { .then(parseXml); }, - listNsdocFiles(): Promise { - const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1'; - return fetch(svsUrl) - .catch(handleError) - .then(handleResponse) - .then(parseXml); - }, - getNsdocFile(id: string): Promise { const svsUrl = getSclValidatorServiceUrl() + '/nsdoc/v1/' + id; return fetch(svsUrl) diff --git a/src/compas/CompasNsdoc.ts b/src/compas/CompasNsdoc.ts index e3bdbfc483..f363f359a8 100644 --- a/src/compas/CompasNsdoc.ts +++ b/src/compas/CompasNsdoc.ts @@ -2,6 +2,7 @@ import { newLoadNsdocEvent } from '../Setting.js'; import { createLogEvent } from '../compas-services/foundation.js'; import { CompasSclValidatorService } from '../compas-services/CompasValidatorService.js'; +import { CompasNSDocFileService } from '../compas-services/CompasNSDocFileService.js'; /** * Load a single entry. Use the nsdocId to look in the Local Storage, if already loaded, @@ -30,7 +31,7 @@ async function processNsdocFile( checksumStored !== checksum ) { console.info(`Loading NSDoc File '${nsdocId}' with ID '${id}'.`); - await CompasSclValidatorService() + await CompasNSDocFileService() .getNsdocFile(id) .then(document => { const nsdocContent = @@ -51,7 +52,7 @@ async function processNsdocFile( * Load each item found using the function #processNsdocFile. */ export async function loadNsdocFiles(component: Element): Promise { - await CompasSclValidatorService() + await CompasNSDocFileService() .listNsdocFiles() .then(response => { Array.from(response.querySelectorAll('NsdocFile') ?? []).forEach( diff --git a/test/unit/compas-services/CompasNSDocFileService.test.ts b/test/unit/compas-services/CompasNSDocFileService.test.ts new file mode 100644 index 0000000000..dedb73fe0e --- /dev/null +++ b/test/unit/compas-services/CompasNSDocFileService.test.ts @@ -0,0 +1,36 @@ +import { expect } from '@open-wc/testing'; + +import { CompasNSDocFileService } from '../../../src/compas-services/CompasNSDocFileService.js'; + +describe('compas-nsdocfile-service', () => { + it('Should list all NSDoc files', async () => { + const res = await CompasNSDocFileService().listNsdocFiles(); + + const nsDocFiles: Element[] = Array.from(res.querySelectorAll('NsdocFile')); + + expect(nsDocFiles.length).to.equal(4); + }); + + it('Should fail on invalid request', done => { + const id = '315b02ac-c4aa-4495-9b4f-f7175a75c315'; + CompasNSDocFileService() + .getNsdocFile(id) + .then(() => done('Failed')) + .catch(err => { + expect(err.status).to.equal(404); + expect(err.type).to.equal('NotFoundError'); + done(); + }); + }); + it('Should fail on invalid id', done => { + const id = '1'; + + CompasNSDocFileService() + .getNsdocFile(id) + .then(() => done('Failed')) + .catch(err => { + expect(err).to.equal(`Unable to find nsDoc file with id ${id}`); + done(); + }); + }); +});