mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Convert data from ResourceStore based on preferences
This commit is contained in:
51
test/unit/storage/PassthroughStore.test.ts
Normal file
51
test/unit/storage/PassthroughStore.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { PassthroughStore } from '../../../src/storage/PassthroughStore';
|
||||
import { Patch } from '../../../src/ldp/http/Patch';
|
||||
import { Representation } from '../../../src/ldp/representation/Representation';
|
||||
import { ResourceStore } from '../../../src/storage/ResourceStore';
|
||||
|
||||
describe('A PassthroughStore', (): void => {
|
||||
let store: PassthroughStore;
|
||||
let source: ResourceStore;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
source = {
|
||||
getRepresentation: jest.fn(async(): Promise<any> => 'get'),
|
||||
addResource: jest.fn(async(): Promise<any> => 'add'),
|
||||
setRepresentation: jest.fn(async(): Promise<any> => 'set'),
|
||||
deleteResource: jest.fn(async(): Promise<any> => 'delete'),
|
||||
modifyResource: jest.fn(async(): Promise<any> => 'modify'),
|
||||
};
|
||||
|
||||
store = new PassthroughStore(source);
|
||||
});
|
||||
|
||||
it('calls getRepresentation directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.getRepresentation({ path: 'getPath' }, {})).resolves.toBe('get');
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: 'getPath' }, {}, undefined);
|
||||
});
|
||||
|
||||
it('calls addResource directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.addResource({ path: 'addPath' }, {} as Representation)).resolves.toBe('add');
|
||||
expect(source.addResource).toHaveBeenCalledTimes(1);
|
||||
expect(source.addResource).toHaveBeenLastCalledWith({ path: 'addPath' }, {}, undefined);
|
||||
});
|
||||
|
||||
it('calls setRepresentation directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.setRepresentation({ path: 'setPath' }, {} as Representation)).resolves.toBe('set');
|
||||
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.setRepresentation).toHaveBeenLastCalledWith({ path: 'setPath' }, {}, undefined);
|
||||
});
|
||||
|
||||
it('calls deleteResource directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.deleteResource({ path: 'deletePath' })).resolves.toBe('delete');
|
||||
expect(source.deleteResource).toHaveBeenCalledTimes(1);
|
||||
expect(source.deleteResource).toHaveBeenLastCalledWith({ path: 'deletePath' }, undefined);
|
||||
});
|
||||
|
||||
it('calls modifyResource directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.modifyResource({ path: 'modifyPath' }, {} as Patch)).resolves.toBe('modify');
|
||||
expect(source.modifyResource).toHaveBeenCalledTimes(1);
|
||||
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'modifyPath' }, {}, undefined);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Patch } from '../../../src/ldp/http/Patch';
|
||||
import { PatchHandler } from '../../../src/storage/patch/PatchHandler';
|
||||
import { PatchingStore } from '../../../src/storage/PatchingStore';
|
||||
import { Representation } from '../../../src/ldp/representation/Representation';
|
||||
import { ResourceStore } from '../../../src/storage/ResourceStore';
|
||||
|
||||
describe('A PatchingStore', (): void => {
|
||||
@@ -12,12 +11,8 @@ describe('A PatchingStore', (): void => {
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
source = {
|
||||
getRepresentation: jest.fn(async(): Promise<any> => 'get'),
|
||||
addResource: jest.fn(async(): Promise<any> => 'add'),
|
||||
setRepresentation: jest.fn(async(): Promise<any> => 'set'),
|
||||
deleteResource: jest.fn(async(): Promise<any> => 'delete'),
|
||||
modifyResource: jest.fn(async(): Promise<any> => 'modify'),
|
||||
};
|
||||
} as unknown as ResourceStore;
|
||||
|
||||
handleSafeFn = jest.fn(async(): Promise<any> => 'patcher');
|
||||
patcher = { handleSafe: handleSafeFn } as unknown as PatchHandler;
|
||||
@@ -25,30 +20,6 @@ describe('A PatchingStore', (): void => {
|
||||
store = new PatchingStore(source, patcher);
|
||||
});
|
||||
|
||||
it('calls getRepresentation directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.getRepresentation({ path: 'getPath' }, {})).resolves.toBe('get');
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.getRepresentation).toHaveBeenLastCalledWith({ path: 'getPath' }, {}, undefined);
|
||||
});
|
||||
|
||||
it('calls addResource directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.addResource({ path: 'addPath' }, {} as Representation)).resolves.toBe('add');
|
||||
expect(source.addResource).toHaveBeenCalledTimes(1);
|
||||
expect(source.addResource).toHaveBeenLastCalledWith({ path: 'addPath' }, {}, undefined);
|
||||
});
|
||||
|
||||
it('calls setRepresentation directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.setRepresentation({ path: 'setPath' }, {} as Representation)).resolves.toBe('set');
|
||||
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.setRepresentation).toHaveBeenLastCalledWith({ path: 'setPath' }, {}, undefined);
|
||||
});
|
||||
|
||||
it('calls deleteResource directly from the source.', async(): Promise<void> => {
|
||||
await expect(store.deleteResource({ path: 'deletePath' })).resolves.toBe('delete');
|
||||
expect(source.deleteResource).toHaveBeenCalledTimes(1);
|
||||
expect(source.deleteResource).toHaveBeenLastCalledWith({ path: 'deletePath' }, undefined);
|
||||
});
|
||||
|
||||
it('calls modifyResource directly from the source if available.', async(): Promise<void> => {
|
||||
await expect(store.modifyResource({ path: 'modifyPath' }, {} as Patch)).resolves.toBe('modify');
|
||||
expect(source.modifyResource).toHaveBeenCalledTimes(1);
|
||||
|
||||
60
test/unit/storage/RepresentationConvertingStore.test.ts
Normal file
60
test/unit/storage/RepresentationConvertingStore.test.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter';
|
||||
import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore';
|
||||
import { ResourceStore } from '../../../src/storage/ResourceStore';
|
||||
|
||||
describe('A RepresentationConvertingStore', (): void => {
|
||||
let store: RepresentationConvertingStore;
|
||||
let source: ResourceStore;
|
||||
let handleSafeFn: jest.Mock<Promise<void>, []>;
|
||||
let converter: RepresentationConverter;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
source = {
|
||||
getRepresentation: jest.fn(async(): Promise<any> => ({ data: 'data', metadata: { contentType: 'text/turtle' }})),
|
||||
} as unknown as ResourceStore;
|
||||
|
||||
handleSafeFn = jest.fn(async(): Promise<any> => 'converter');
|
||||
converter = { handleSafe: handleSafeFn } as unknown as RepresentationConverter;
|
||||
|
||||
store = new RepresentationConvertingStore(source, converter);
|
||||
});
|
||||
|
||||
it('returns the Representation from the source if no changes are required.', async(): Promise<void> => {
|
||||
await expect(store.getRepresentation({ path: 'path' }, { type: [
|
||||
{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 },
|
||||
]})).resolves.toEqual({
|
||||
data: 'data',
|
||||
metadata: { contentType: 'text/turtle' },
|
||||
});
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.getRepresentation).toHaveBeenLastCalledWith(
|
||||
{ path: 'path' }, { type: [{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]}, undefined,
|
||||
);
|
||||
expect(handleSafeFn).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('returns the Representation from the source if there are no preferences.', async(): Promise<void> => {
|
||||
await expect(store.getRepresentation({ path: 'path' }, {})).resolves.toEqual({
|
||||
data: 'data',
|
||||
metadata: { contentType: 'text/turtle' },
|
||||
});
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.getRepresentation).toHaveBeenLastCalledWith(
|
||||
{ path: 'path' }, {}, undefined,
|
||||
);
|
||||
expect(handleSafeFn).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('calls the converter if another output is preferred.', async(): Promise<void> => {
|
||||
await expect(store.getRepresentation({ path: 'path' }, { type: [
|
||||
{ value: 'text/plain', weight: 1 }, { value: 'text/turtle', weight: 0 },
|
||||
]})).resolves.toEqual('converter');
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(handleSafeFn).toHaveBeenCalledTimes(1);
|
||||
expect(handleSafeFn).toHaveBeenLastCalledWith({
|
||||
identifier: { path: 'path' },
|
||||
representation: { data: 'data', metadata: { contentType: 'text/turtle' }},
|
||||
preferences: { type: [{ value: 'text/plain', weight: 1 }, { value: 'text/turtle', weight: 0 }]},
|
||||
});
|
||||
});
|
||||
});
|
||||
52
test/unit/storage/conversion/ConversionUtil.test.ts
Normal file
52
test/unit/storage/conversion/ConversionUtil.test.ts
Normal 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 }]);
|
||||
});
|
||||
});
|
||||
});
|
||||
43
test/unit/storage/conversion/QuadToTurtleConverter.test.ts
Normal file
43
test/unit/storage/conversion/QuadToTurtleConverter.test.ts
Normal 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>',
|
||||
);
|
||||
});
|
||||
});
|
||||
59
test/unit/storage/conversion/TurtleToQuadConverter.test.ts
Normal file
59
test/unit/storage/conversion/TurtleToQuadConverter.test.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
34
test/unit/util/Util.test.ts
Normal file
34
test/unit/util/Util.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import streamifyArray from 'streamify-array';
|
||||
import { ensureTrailingSlash, matchingMediaType, readableToString } from '../../../src/util/Util';
|
||||
|
||||
describe('Util function', (): void => {
|
||||
describe('ensureTrailingSlash', (): void => {
|
||||
it('makes sure there is always exactly 1 slash.', async(): Promise<void> => {
|
||||
expect(ensureTrailingSlash('http://test.com')).toEqual('http://test.com/');
|
||||
expect(ensureTrailingSlash('http://test.com/')).toEqual('http://test.com/');
|
||||
expect(ensureTrailingSlash('http://test.com//')).toEqual('http://test.com/');
|
||||
expect(ensureTrailingSlash('http://test.com///')).toEqual('http://test.com/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('readableToString', (): void => {
|
||||
it('concatenates all elements of a Readable.', async(): Promise<void> => {
|
||||
const stream = streamifyArray([ 'a', 'b', 'c' ]);
|
||||
await expect(readableToString(stream)).resolves.toEqual('abc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchingMediaType', (): void => {
|
||||
it('matches all possible media types.', async(): Promise<void> => {
|
||||
expect(matchingMediaType('*/*', 'text/turtle')).toBeTruthy();
|
||||
expect(matchingMediaType('text/*', '*/*')).toBeTruthy();
|
||||
expect(matchingMediaType('text/*', 'text/turtle')).toBeTruthy();
|
||||
expect(matchingMediaType('text/plain', 'text/*')).toBeTruthy();
|
||||
expect(matchingMediaType('text/turtle', 'text/turtle')).toBeTruthy();
|
||||
|
||||
expect(matchingMediaType('text/*', 'application/*')).toBeFalsy();
|
||||
expect(matchingMediaType('text/plain', 'application/*')).toBeFalsy();
|
||||
expect(matchingMediaType('text/plain', 'text/turtle')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user