import { namedNode, triple } from '@rdfjs/data-model'; import rdfSerializer from 'rdf-serialize'; import stringifyStream from 'stream-to-string'; import streamifyArray from 'streamify-array'; import { Representation } from '../../../../src/ldp/representation/Representation'; import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences'; import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier'; import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; describe('A QuadToRdfConverter', (): void => { const converter = new QuadToRdfConverter(); const identifier: ResourceIdentifier = { path: 'path' }; it('supports parsing quads.', async(): Promise => { await expect(converter.getInputTypes()).resolves.toEqual({ [INTERNAL_QUADS]: 1 }); }); it('supports serializing as the same types as rdfSerializer.', async(): Promise => { await expect(converter.getOutputTypes()).resolves.toEqual(await rdfSerializer.getContentTypesPrioritized()); }); it('can handle quad to turtle conversions.', async(): Promise => { const representation = { metadata: { contentType: INTERNAL_QUADS }} as Representation; const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]}; await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined(); }); it('can handle quad to JSON-LD conversions.', async(): Promise => { const representation = { metadata: { contentType: INTERNAL_QUADS }} as Representation; const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]}; await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined(); }); it('converts quads to turtle.', async(): Promise => { const representation = { data: streamifyArray([ triple( namedNode('http://test.com/s'), namedNode('http://test.com/p'), namedNode('http://test.com/o'), ) ]), metadata: { contentType: INTERNAL_QUADS }, } as Representation; const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toMatchObject({ binary: true, metadata: { contentType: 'text/turtle', }, }); await expect(stringifyStream(result.data)).resolves.toEqual( ` . `, ); }); it('converts quads to JSON-LD.', async(): Promise => { const representation = { data: streamifyArray([ triple( namedNode('http://test.com/s'), namedNode('http://test.com/p'), namedNode('http://test.com/o'), ) ]), metadata: { contentType: INTERNAL_QUADS }, } as Representation; const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]}; const result = await converter.handle({ identifier, representation, preferences }); expect(result).toMatchObject({ binary: true, metadata: { contentType: 'application/ld+json', }, }); await expect(stringifyStream(result.data)).resolves.toEqual( `[ { "@id": "http://test.com/s", "http://test.com/p": [ { "@id": "http://test.com/o" } ] } ] `, ); }); });