import type { Readable } from 'stream'; import arrayifyStream from 'arrayify-stream'; import { SparqlEndpointFetcher } from 'fetch-sparql-endpoint'; import { DataFactory } from 'n3'; import type { Quad } from 'rdf-js'; import streamifyArray from 'streamify-array'; import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata'; import { SparqlDataAccessor } from '../../../../src/storage/accessors/SparqlDataAccessor'; import { UrlContainerManager } from '../../../../src/storage/UrlContainerManager'; import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes'; import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError'; import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; import { MetadataController } from '../../../../src/util/MetadataController'; import { CONTENT_TYPE, LDP, RDF } from '../../../../src/util/UriConstants'; import { toNamedNode } from '../../../../src/util/UriUtil'; const { literal, namedNode, quad } = DataFactory; jest.mock('fetch-sparql-endpoint'); const simplifyQuery = (query: string | string[]): string => { if (Array.isArray(query)) { query = query.join(' '); } return query.replace(/\n/gu, ' ').trim(); }; describe('A SparqlDataAccessor', (): void => { const endpoint = 'http://test.com/sparql'; const base = 'http://test.com/'; let accessor: SparqlDataAccessor; let metadata: RepresentationMetadata; let fetchTriples: jest.Mock>; let fetchUpdate: jest.Mock>; let triples: Quad[]; let fetchError: any; let updateError: any; beforeEach(async(): Promise => { metadata = new RepresentationMetadata(); triples = [ quad(namedNode('this'), namedNode('a'), namedNode('triple')) ]; // Makes it so the `SparqlEndpointFetcher` will always return the contents of the `triples` array fetchTriples = jest.fn(async(): Promise => { if (fetchError) { throw fetchError; } return streamifyArray(triples); }); fetchUpdate = jest.fn(async(): Promise => { if (updateError) { throw updateError; } }); (SparqlEndpointFetcher as any).mockImplementation((): any => ({ fetchTriples, fetchUpdate, })); // This needs to be last so the fetcher can be mocked first accessor = new SparqlDataAccessor(endpoint, base, new UrlContainerManager(base), new MetadataController()); }); it('can only handle quad data.', async(): Promise => { const data = streamifyArray([]); await expect(accessor.canHandle({ binary: true, data, metadata })).rejects.toThrow(UnsupportedMediaTypeHttpError); metadata.contentType = 'newInternalType'; await expect(accessor.canHandle({ binary: false, data, metadata })).rejects.toThrow(UnsupportedMediaTypeHttpError); metadata.contentType = INTERNAL_QUADS; await expect(accessor.canHandle({ binary: false, data, metadata })).resolves.toBeUndefined(); }); it('returns the corresponding quads when data is requested.', async(): Promise => { const data = await accessor.getData({ path: 'http://identifier' }); await expect(arrayifyStream(data)).resolves.toBeRdfIsomorphic([ quad(namedNode('this'), namedNode('a'), namedNode('triple')), ]); expect(fetchTriples).toHaveBeenCalledTimes(1); expect(fetchTriples.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery( 'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH { ?s ?p ?o. } }', )); }); it('returns the corresponding metadata when requested.', async(): Promise => { metadata = await accessor.getMetadata({ path: 'http://identifier' }); expect(metadata.quads()).toBeRdfIsomorphic([ quad(namedNode('this'), namedNode('a'), namedNode('triple')), quad(namedNode('http://identifier'), toNamedNode(CONTENT_TYPE), literal(INTERNAL_QUADS)), ]); expect(fetchTriples).toHaveBeenCalledTimes(1); expect(fetchTriples.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery( 'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH { ?s ?p ?o. } }', )); }); it('requests container data for generating its metadata.', async(): Promise => { metadata = await accessor.getMetadata({ path: 'http://container/' }); expect(metadata.quads()).toBeRdfIsomorphic([ quad(namedNode('this'), namedNode('a'), namedNode('triple')), quad(namedNode('http://container/'), toNamedNode(CONTENT_TYPE), literal(INTERNAL_QUADS)), ]); expect(fetchTriples).toHaveBeenCalledTimes(1); expect(fetchTriples.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery([ 'CONSTRUCT { ?s ?p ?o. } WHERE {', ' { GRAPH { ?s ?p ?o. } }', ' UNION', ' { GRAPH { ?s ?p ?o. } }', '}', ])); }); it('generates resource metadata for the root container.', async(): Promise => { metadata = await accessor.getMetadata({ path: base }); expect(metadata.quads()).toBeRdfIsomorphic([ quad(namedNode('this'), namedNode('a'), namedNode('triple')), quad(namedNode(base), toNamedNode(CONTENT_TYPE), literal(INTERNAL_QUADS)), quad(namedNode(base), toNamedNode(RDF.type), toNamedNode(LDP.Container)), quad(namedNode(base), toNamedNode(RDF.type), toNamedNode(LDP.BasicContainer)), quad(namedNode(base), toNamedNode(RDF.type), toNamedNode(LDP.Resource)), ]); expect(fetchTriples).toHaveBeenCalledTimes(1); expect(fetchTriples.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery([ 'CONSTRUCT { ?s ?p ?o. } WHERE {', ` { GRAPH <${base}> { ?s ?p ?o. } }`, ' UNION', ` { GRAPH { ?s ?p ?o. } }`, '}', ])); }); it('throws 404 if no metadata was found.', async(): Promise => { // Clear triples array triples = []; await expect(accessor.getMetadata({ path: 'http://identifier' })).rejects.toThrow(NotFoundHttpError); expect(fetchTriples).toHaveBeenCalledTimes(1); expect(fetchTriples.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchTriples.mock.calls[0][1])).toBe(simplifyQuery( 'CONSTRUCT { ?s ?p ?o. } WHERE { GRAPH { ?s ?p ?o. } }', )); }); it('overwrites the metadata when writing a container and updates parent.', async(): Promise => { metadata = new RepresentationMetadata('http://test.com/container/', { [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]}); await expect(accessor.writeContainer({ path: 'http://test.com/container/' }, metadata)).resolves.toBeUndefined(); expect(fetchUpdate).toHaveBeenCalledTimes(1); expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([ 'DELETE WHERE { GRAPH { ?s ?p ?o. } };', 'INSERT DATA {', ' GRAPH {', ' .', ' .', ' }', ' GRAPH { . }', '}', ])); }); it('overwrites the data and metadata when writing a resource and updates parent.', async(): Promise => { const data = streamifyArray([ quad(namedNode('http://name'), namedNode('http://pred'), literal('value')) ]); metadata = new RepresentationMetadata('http://test.com/container/resource', { [RDF.type]: [ toNamedNode(LDP.Resource) ]}); await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata)) .resolves.toBeUndefined(); expect(fetchUpdate).toHaveBeenCalledTimes(1); expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([ 'DELETE WHERE { GRAPH { ?s ?p ?o. } };', 'DELETE WHERE { GRAPH { ?s ?p ?o. } };', 'INSERT DATA {', ' GRAPH { . }', ' GRAPH { . }', ' GRAPH { "value". }', '}', ])); }); it('removes all references when deleting a resource.', async(): Promise => { metadata = new RepresentationMetadata('http://test.com/container/', { [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]}); await expect(accessor.deleteResource({ path: 'http://test.com/container/' })).resolves.toBeUndefined(); expect(fetchUpdate).toHaveBeenCalledTimes(1); expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint); expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([ 'DELETE WHERE { GRAPH { ?s ?p ?o. } };', 'DELETE WHERE { GRAPH { ?s ?p ?o. } };', 'DELETE DATA { GRAPH { . } }', ])); }); it('errors when trying to write to a metadata document.', async(): Promise => { const data = streamifyArray([ quad(namedNode('http://name'), namedNode('http://pred'), literal('value')) ]); await expect(accessor.writeDocument({ path: 'meta:http://test.com/container/resource' }, data, metadata)) .rejects.toThrow(new ConflictHttpError('Not allowed to create NamedNodes with the metadata extension.')); }); it('errors when writing triples in a non-default graph.', async(): Promise => { const data = streamifyArray( [ quad(namedNode('http://name'), namedNode('http://pred'), literal('value'), namedNode('badGraph!')) ], ); await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata)) .rejects.toThrow(new UnsupportedHttpError('Only triples in the default graph are supported.')); }); it('errors when the SPARQL endpoint fails during reading.', async(): Promise => { fetchError = 'error'; await expect(accessor.getMetadata({ path: 'http://identifier' })).rejects.toBe(fetchError); fetchError = new Error(); await expect(accessor.getMetadata({ path: 'http://identifier' })).rejects.toThrow(fetchError); fetchError = undefined; }); it('errors when the SPARQL endpoint fails during writing.', async(): Promise => { const path = 'http://test.com/container/'; metadata = new RepresentationMetadata(path); updateError = 'error'; await expect(accessor.writeContainer({ path }, metadata)).rejects.toBe(updateError); updateError = new Error(); await expect(accessor.writeContainer({ path }, metadata)).rejects.toThrow(updateError); updateError = undefined; }); });