feat: Convert data from ResourceStore based on preferences

This commit is contained in:
Joachim Van Herwegen
2020-08-04 10:23:00 +02:00
parent d6a35f9954
commit 5e1bb10f81
17 changed files with 565 additions and 79 deletions

View File

@@ -0,0 +1,52 @@
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { checkRequest, matchingTypes } from '../../../../src/storage/conversion/ConversionUtil';
describe('A ConversionUtil', (): void => {
const identifier: ResourceIdentifier = { path: 'path' };
describe('#checkRequest', (): void => {
it('requires an input type.', async(): Promise<void> => {
const representation = { metadata: {}} as Representation;
const preferences: RepresentationPreferences = {};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toThrow('Input type required for conversion.');
});
it('requires a matching input type.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'a/x' }} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ 'c/x' ], [ '*/*' ]))
.toThrow('Can only convert from c/x to */*.');
});
it('requires a matching output type.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'a/x' }} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ 'c/x' ]))
.toThrow('Can only convert from */* to c/x.');
});
it('succeeds with a valid input and output type.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'a/x' }} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect(checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toBeUndefined();
});
});
describe('#matchingTypes', (): void => {
it('requires type preferences.', async(): Promise<void> => {
const preferences: RepresentationPreferences = {};
expect((): any => matchingTypes(preferences, [ '*/*' ]))
.toThrow('Output type required for conversion.');
});
it('returns matching types if weight > 0.', async(): Promise<void> => {
const preferences: RepresentationPreferences = { type:
[{ value: 'a/x', weight: 1 }, { value: 'b/x', weight: 0.5 }, { value: 'c/x', weight: 0 }]};
expect(matchingTypes(preferences, [ 'b/x', 'c/x' ])).toEqual([{ value: 'b/x', weight: 0.5 }]);
});
});
});

View File

@@ -0,0 +1,43 @@
import arrayifyStream from 'arrayify-stream';
import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter';
import { Readable } from 'stream';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import streamifyArray from 'streamify-array';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
import { namedNode, triple } from '@rdfjs/data-model';
describe('A QuadToTurtleConverter', (): void => {
const converter = new QuadToTurtleConverter();
const identifier: ResourceIdentifier = { path: 'path' };
it('can handle quad to turtle conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: CONTENT_TYPE_QUADS }} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('converts quads to turtle.', async(): Promise<void> => {
const representation = {
data: streamifyArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]),
metadata: { contentType: CONTENT_TYPE_QUADS },
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
data: expect.any(Readable),
dataType: DATA_TYPE_BINARY,
metadata: {
contentType: 'text/turtle',
},
});
await expect(arrayifyStream(result.data)).resolves.toContain(
'<http://test.com/s> <http://test.com/p> <http://test.com/o>',
);
});
});

View File

@@ -0,0 +1,59 @@
import arrayifyStream from 'arrayify-stream';
import { Readable } from 'stream';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import streamifyArray from 'streamify-array';
import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { namedNode, triple } from '@rdfjs/data-model';
describe('A TurtleToQuadConverter', (): void => {
const converter = new TurtleToQuadConverter();
const identifier: ResourceIdentifier = { path: 'path' };
it('can handle turtle to quad conversions.', async(): Promise<void> => {
const representation = { metadata: { contentType: 'text/turtle' }} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('converts turtle to quads.', async(): Promise<void> => {
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata: { contentType: 'text/turtle' },
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},
});
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]);
});
it('throws an UnsupportedHttpError on invalid triple data.', async(): Promise<void> => {
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.co' ]),
metadata: { contentType: 'text/turtle' },
} as Representation;
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},
});
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
});
});