refactor: Streamline RepresentationMetadata interface

This commit is contained in:
Joachim Van Herwegen
2020-09-08 09:43:30 +02:00
parent 76319ba360
commit 8d3979372b
36 changed files with 416 additions and 230 deletions

View File

@@ -17,7 +17,7 @@ import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/Unsuppor
import { InteractionController } from '../../../src/util/InteractionController';
import { LINK_TYPE_LDP_BC, LINK_TYPE_LDPR } from '../../../src/util/LinkTypes';
import { MetadataController } from '../../../src/util/MetadataController';
import { BYTE_SIZE, CONTENT_TYPE, LAST_CHANGED, SLUG, TYPE } from '../../../src/util/MetadataTypes';
import { HTTP_BYTE_SIZE, HTTP_LAST_CHANGED, HTTP_SLUG, RDF_TYPE } from '../../../src/util/MetadataTypes';
import { LDP, RDF, STAT, TERMS, XML } from '../../../src/util/Prefixes';
const { join: joinPath } = posix;
@@ -133,8 +133,8 @@ describe('A FileResourceStore', (): void => {
(fs.createReadStream as jest.Mock).mockImplementationOnce((): any => new Error('Metadata file does not exist.'));
// Write container (POST)
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(HTTP_SLUG, 'myContainer/');
const identifier = await store.addResource({ path: base }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'myContainer/'), { recursive: true });
expect(identifier.path).toBe(`${base}myContainer/`);
@@ -146,8 +146,8 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toBeDefined();
});
@@ -156,8 +156,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(HTTP_SLUG, 'myContainer/');
await expect(store.addResource({ path: `${base}foo` }, representation)).rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
});
@@ -173,19 +173,19 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'myContainer/');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(HTTP_SLUG, 'myContainer/');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
representation.metadata.set(TYPE, LINK_TYPE_LDPR);
representation.metadata.set(SLUG, 'file.txt');
representation.metadata.set(RDF_TYPE, LINK_TYPE_LDPR);
representation.metadata.set(HTTP_SLUG, 'file.txt');
await expect(store.addResource({ path: `${base}doesnotexist` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexist'));
representation.metadata.removeAll(TYPE);
representation.metadata.removeAll(RDF_TYPE);
await expect(store.addResource({ path: `${base}existingresource` }, representation))
.rejects.toThrow(MethodNotAllowedHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'existingresource'));
@@ -217,9 +217,9 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/plain');
expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(HTTP_BYTE_SIZE)?.value).toEqual(`${stats.size}`);
expect(result.metadata.contentType).toEqual('text/plain');
await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
@@ -254,8 +254,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.metadata.add(SLUG, 'file.txt');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
representation.metadata.add(HTTP_SLUG, 'file.txt');
const identifier = await store.addResource({ path: `${base}doesnotexistyet/` }, representation);
expect(identifier.path).toBe(`${base}doesnotexistyet/file.txt`);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'doesnotexistyet/'),
@@ -280,7 +280,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
representation.data = readableMock;
await store.addResource({ path: `${base}foo/` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'), { recursive: true });
@@ -368,8 +368,8 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray(quads);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
expect(fsPromises.readdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
@@ -387,7 +387,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.lstat as jest.Mock).mockReturnValueOnce(stats);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
await store.setRepresentation({ path: `${base}alreadyexists.txt` }, representation);
expect(fs.createWriteStream as jest.Mock).toBeCalledTimes(2);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists.txt'));
@@ -403,7 +403,7 @@ describe('A FileResourceStore', (): void => {
await expect(store.setRepresentation({ path: `${base}alreadyexists` }, representation)).rejects
.toThrow(ConflictHttpError);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'alreadyexists'));
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
await expect(store.setRepresentation({ path: `${base}alreadyexists/` }, representation)).rejects
.toThrow(ConflictHttpError);
expect(fsPromises.access as jest.Mock).toBeCalledTimes(1);
@@ -417,7 +417,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
await store.setRepresentation({ path: `${base}foo/` }, representation);
expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1);
expect(fsPromises.access as jest.Mock).toBeCalledTimes(1);
@@ -435,8 +435,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.unlink as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDPR);
representation.metadata.add(SLUG, 'file.txt');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDPR);
representation.metadata.add(HTTP_SLUG, 'file.txt');
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error);
expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt.metadata'));
expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
@@ -452,8 +452,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.rmdir as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'foo/');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(HTTP_SLUG, 'foo/');
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(Error);
expect(fsPromises.rmdir as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo/'));
});
@@ -464,7 +464,7 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValueOnce(true);
// Tests
representation.metadata.add(SLUG, 'myContainer/');
representation.metadata.add(HTTP_SLUG, 'myContainer/');
const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path).toBe(`${base}myContainer/`);
expect(fsPromises.mkdir as jest.Mock).toBeCalledTimes(1);
@@ -484,9 +484,9 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/octet-stream');
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`);
expect(result.metadata.contentType).toEqual('application/octet-stream');
expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(HTTP_BYTE_SIZE)?.value).toEqual(`${stats.size}`);
});
it('errors when performing a PUT on the root path.', async(): Promise<void> => {
@@ -519,8 +519,8 @@ describe('A FileResourceStore', (): void => {
(fsPromises.mkdir as jest.Mock).mockReturnValue(true);
// Tests
representation.metadata.add(TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(SLUG, 'bar');
representation.metadata.add(RDF_TYPE, LINK_TYPE_LDP_BC);
representation.metadata.add(HTTP_SLUG, 'bar');
const identifier = await store.addResource({ path: `${base}foo` }, representation);
expect(identifier.path).toBe(`${base}foo/bar/`);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'foo'));
@@ -552,8 +552,8 @@ describe('A FileResourceStore', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(BYTE_SIZE)?.value).toEqual(`${stats.size}`);
expect(result.metadata.get(HTTP_LAST_CHANGED)?.value).toEqual(stats.mtime.toISOString());
expect(result.metadata.get(HTTP_BYTE_SIZE)?.value).toEqual(`${stats.size}`);
await expect(arrayifyStream(result.data)).resolves.toEqual([ rawData ]);
expect(fsPromises.lstat as jest.Mock).toBeCalledWith(joinPath(rootFilepath, name));
expect(fs.createReadStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, name));

View File

@@ -2,15 +2,14 @@ import { RepresentationMetadata } from '../../../src/ldp/representation/Represen
import { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter';
import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore';
import { ResourceStore } from '../../../src/storage/ResourceStore';
import { CONTENT_TYPE } from '../../../src/util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../../src/util/MetadataTypes';
describe('A RepresentationConvertingStore', (): void => {
let store: RepresentationConvertingStore;
let source: ResourceStore;
let handleSafeFn: jest.Mock<Promise<void>, []>;
let converter: RepresentationConverter;
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'text/turtle');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
beforeEach(async(): Promise<void> => {
source = {
@@ -31,7 +30,7 @@ describe('A RepresentationConvertingStore', (): void => {
data: 'data',
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(result.metadata.contentType).toEqual('text/turtle');
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, { type: [{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]}, undefined,
@@ -45,7 +44,7 @@ describe('A RepresentationConvertingStore', (): void => {
data: 'data',
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(result.metadata.contentType).toEqual('text/turtle');
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, {}, undefined,

View File

@@ -5,7 +5,7 @@ import { ChainedConverter } from '../../../../src/storage/conversion/ChainedConv
import { checkRequest } from '../../../../src/storage/conversion/ConversionUtil';
import { RepresentationConverterArgs } from '../../../../src/storage/conversion/RepresentationConverter';
import { TypedRepresentationConverter } from '../../../../src/storage/conversion/TypedRepresentationConverter';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
class DummyConverter extends TypedRepresentationConverter {
private readonly inTypes: { [contentType: string]: number };
@@ -30,9 +30,8 @@ class DummyConverter extends TypedRepresentationConverter {
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
const oldMeta = input.representation.metadata;
const metadata = new RepresentationMetadata(oldMeta.identifier, oldMeta.quads());
metadata.set(CONTENT_TYPE, input.preferences.type![0].value);
const metadata = new RepresentationMetadata(input.representation.metadata,
{ [MA_CONTENT_TYPE]: input.preferences.type![0].value });
return { ...input.representation, metadata };
}
}
@@ -52,8 +51,7 @@ describe('A ChainedConverter', (): void => {
];
converter = new ChainedConverter(converters);
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
representation = { metadata } as Representation;
preferences = { type: [{ value: 'internal/quads', weight: 1 }]};
args = { representation, preferences, identifier: { path: 'path' }};
@@ -79,7 +77,7 @@ describe('A ChainedConverter', (): void => {
});
it('errors if the start of the chain does not support the representation type.', async(): Promise<void> => {
representation.metadata.set(CONTENT_TYPE, 'bad/type');
representation.metadata.contentType = 'bad/type';
await expect(converter.canHandle(args)).rejects.toThrow();
});
@@ -94,7 +92,7 @@ describe('A ChainedConverter', (): void => {
jest.spyOn(converters[2], 'handle');
const result = await converter.handle(args);
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('internal/quads');
expect(result.metadata.contentType).toEqual('internal/quads');
expect((converters[0] as any).handle).toHaveBeenCalledTimes(1);
expect((converters[1] as any).handle).toHaveBeenCalledTimes(1);
expect((converters[2] as any).handle).toHaveBeenCalledTimes(1);

View File

@@ -3,7 +3,6 @@ import { RepresentationMetadata } from '../../../../src/ldp/representation/Repre
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { checkRequest, matchingTypes } from '../../../../src/storage/conversion/ConversionUtil';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A ConversionUtil', (): void => {
const identifier: ResourceIdentifier = { path: 'path' };
@@ -23,21 +22,21 @@ describe('A ConversionUtil', (): void => {
});
it('requires a matching input type.', async(): Promise<void> => {
metadata.add(CONTENT_TYPE, 'a/x');
metadata.contentType = 'a/x';
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> => {
metadata.add(CONTENT_TYPE, 'a/x');
metadata.contentType = 'a/x';
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> => {
metadata.add(CONTENT_TYPE, 'a/x');
metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect(checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toBeUndefined();

View File

@@ -8,13 +8,12 @@ import { RepresentationPreferences } from '../../../../src/ldp/representation/Re
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A QuadToRdfConverter', (): void => {
const converter = new QuadToRdfConverter();
const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: INTERNAL_QUADS });
it('supports parsing quads.', async(): Promise<void> => {
await expect(converter.getInputTypes()).resolves.toEqual({ [INTERNAL_QUADS]: 1 });
@@ -51,7 +50,7 @@ describe('A QuadToRdfConverter', (): void => {
binary: true,
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(result.metadata.contentType).toEqual('text/turtle');
await expect(stringifyStream(result.data)).resolves.toEqual(
`<http://test.com/s> <http://test.com/p> <http://test.com/o>.
`,
@@ -59,7 +58,7 @@ describe('A QuadToRdfConverter', (): void => {
});
it('converts quads to JSON-LD.', async(): Promise<void> => {
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
metadata.contentType = INTERNAL_QUADS;
const representation = {
data: streamifyArray([ triple(
namedNode('http://test.com/s'),
@@ -74,7 +73,7 @@ describe('A QuadToRdfConverter', (): void => {
binary: true,
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('application/ld+json');
expect(result.metadata.contentType).toEqual('application/ld+json');
await expect(stringifyStream(result.data)).resolves.toEqual(
`[
{

View File

@@ -7,13 +7,12 @@ import { RepresentationPreferences } from '../../../../src/ldp/representation/Re
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A QuadToTurtleConverter', (): void => {
const converter = new QuadToTurtleConverter();
const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: INTERNAL_QUADS });
it('can handle quad to turtle conversions.', async(): Promise<void> => {
const representation = { metadata } as Representation;
@@ -36,7 +35,7 @@ describe('A QuadToTurtleConverter', (): void => {
binary: true,
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual('text/turtle');
expect(result.metadata.contentType).toEqual('text/turtle');
await expect(arrayifyStream(result.data)).resolves.toContain(
'<http://test.com/s> <http://test.com/p> <http://test.com/o>',
);

View File

@@ -10,7 +10,7 @@ import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceI
import { RdfToQuadConverter } from '../../../../src/storage/conversion/RdfToQuadConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A RdfToQuadConverter.test.ts', (): void => {
const converter = new RdfToQuadConverter();
@@ -25,24 +25,21 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
});
it('can handle turtle to quad conversions.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('can handle JSON-LD to quad conversions.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'application/ld+json');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/ld+json' });
const representation = { metadata } as Representation;
const preferences: RepresentationPreferences = { type: [{ value: INTERNAL_QUADS, weight: 1 }]};
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
});
it('converts turtle to quads.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
metadata,
@@ -54,7 +51,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
@@ -63,8 +60,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
});
it('converts JSON-LD to quads.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'application/ld+json');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'application/ld+json' });
const representation = {
data: streamifyArray([ '{"@id": "http://test.com/s", "http://test.com/p": { "@id": "http://test.com/o" }}' ]),
metadata,
@@ -76,7 +72,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
@@ -85,8 +81,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
});
it('throws an UnsupportedHttpError on invalid triple data.', async(): Promise<void> => {
const metadata = new RepresentationMetadata();
metadata.set(CONTENT_TYPE, 'text/turtle');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
const representation = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.co' ]),
metadata,
@@ -98,7 +93,7 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
});
});

View File

@@ -9,13 +9,12 @@ import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceI
import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A TurtleToQuadConverter', (): void => {
const converter = new TurtleToQuadConverter();
const identifier: ResourceIdentifier = { path: 'path' };
const metadata = new RepresentationMetadata();
metadata.add(CONTENT_TYPE, 'text/turtle');
const metadata = new RepresentationMetadata({ [MA_CONTENT_TYPE]: 'text/turtle' });
it('can handle turtle to quad conversions.', async(): Promise<void> => {
const representation = { metadata } as Representation;
@@ -35,7 +34,7 @@ describe('A TurtleToQuadConverter', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
@@ -55,7 +54,7 @@ describe('A TurtleToQuadConverter', (): void => {
data: expect.any(Readable),
metadata: expect.any(RepresentationMetadata),
});
expect(result.metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(result.metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(result.data)).rejects.toThrow(UnsupportedHttpError);
});
});

View File

@@ -11,7 +11,6 @@ import { ResourceLocker } from '../../../../src/storage/ResourceLocker';
import { ResourceStore } from '../../../../src/storage/ResourceStore';
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../../../src/util/MetadataTypes';
describe('A SparqlUpdatePatchHandler', (): void => {
let handler: SparqlUpdatePatchHandler;
@@ -77,7 +76,7 @@ describe('A SparqlUpdatePatchHandler', (): void => {
binary: false,
metadata: expect.any(RepresentationMetadata),
}));
expect(setParams[1].metadata.get(CONTENT_TYPE)?.value).toEqual(INTERNAL_QUADS);
expect(setParams[1].metadata.contentType).toEqual(INTERNAL_QUADS);
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);
};