refactor: Replace dataType by binary flag

This commit is contained in:
Ruben Verborgh
2020-09-02 23:02:03 +02:00
committed by Joachim Van Herwegen
parent 385e1a4cdf
commit c5c5d13570
33 changed files with 75 additions and 112 deletions

View File

@@ -4,7 +4,6 @@ import { Quad } from 'rdf-js';
import streamifyArray from 'streamify-array';
import { BasicResponseWriter } from '../../../../src/ldp/http/BasicResponseWriter';
import { ResponseDescription } from '../../../../src/ldp/operations/ResponseDescription';
import { DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A BasicResponseWriter', (): void => {
@@ -16,9 +15,9 @@ describe('A BasicResponseWriter', (): void => {
});
it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => {
await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_QUAD }} as ResponseDescription }))
await expect(writer.canHandle({ response, result: { body: { binary: false }} as ResponseDescription }))
.rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_BINARY }} as ResponseDescription }))
await expect(writer.canHandle({ response, result: { body: { binary: true }} as ResponseDescription }))
.resolves.toBeUndefined();
});
@@ -31,8 +30,8 @@ describe('A BasicResponseWriter', (): void => {
it('responds with a body if the description has a body.', async(done): Promise<void> => {
const body = {
binary: true,
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
dataType: DATA_TYPE_BINARY,
metadata: {
raw: [] as Quad[],
profiles: [] as string[],
@@ -52,8 +51,8 @@ describe('A BasicResponseWriter', (): void => {
it('responds with a content-type if the metadata has it.', async(done): Promise<void> => {
const body = {
binary: true,
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
dataType: DATA_TYPE_BINARY,
metadata: {
raw: [] as Quad[],
profiles: [] as string[],

View File

@@ -2,7 +2,6 @@ import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { RawBodyParser } from '../../../../src/ldp/http/RawBodyParser';
import { HttpRequest } from '../../../../src/server/HttpRequest';
import { DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import 'jest-rdf';
@@ -22,8 +21,8 @@ describe('A RawBodyparser', (): void => {
input.headers = { 'content-type': 'text/turtle' };
const result = (await bodyParser.handle(input))!;
expect(result).toEqual({
binary: true,
data: input,
dataType: DATA_TYPE_BINARY,
metadata: {
contentType: 'text/turtle',
raw: [],

View File

@@ -4,7 +4,6 @@ import { Algebra } from 'sparqlalgebrajs';
import streamifyArray from 'streamify-array';
import { SparqlUpdateBodyParser } from '../../../../src/ldp/http/SparqlUpdateBodyParser';
import { HttpRequest } from '../../../../src/server/HttpRequest';
import { DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
@@ -34,7 +33,7 @@ describe('A SparqlUpdateBodyParser', (): void => {
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]);
expect(result.dataType).toBe(DATA_TYPE_BINARY);
expect(result.binary).toBe(true);
expect(result.metadata).toEqual({
raw: [],
profiles: [],

View File

@@ -2,12 +2,11 @@ import { GetOperationHandler } from '../../../../src/ldp/operations/GetOperation
import { Operation } from '../../../../src/ldp/operations/Operation';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { ResourceStore } from '../../../../src/storage/ResourceStore';
import { DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A GetOperationHandler', (): void => {
const store = {
getRepresentation: async(): Promise<Representation> => ({ dataType: DATA_TYPE_QUAD } as Representation),
getRepresentation: async(): Promise<Representation> => ({ binary: false } as Representation),
} as unknown as ResourceStore;
const handler = new GetOperationHandler(store);
@@ -18,7 +17,7 @@ describe('A GetOperationHandler', (): void => {
it('returns the representation from the store with the input identifier.', async(): Promise<void> => {
await expect(handler.handle({ target: { path: 'url' }} as Operation)).resolves.toEqual(
{ identifier: { path: 'url' }, body: { dataType: DATA_TYPE_QUAD }},
{ identifier: { path: 'url' }, body: { binary: false }},
);
});
});

View File

@@ -16,9 +16,9 @@ describe('A PatchOperationHandler', (): void => {
});
it('deletes the resource from the store and returns its identifier.', async(): Promise<void> => {
await expect(handler.handle({ target: { path: 'url' }, body: { dataType: 'patch' }} as Operation))
await expect(handler.handle({ target: { path: 'url' }, body: { binary: false }} as Operation))
.resolves.toEqual({ identifier: { path: 'url' }});
expect(store.modifyResource).toHaveBeenCalledTimes(1);
expect(store.modifyResource).toHaveBeenLastCalledWith({ path: 'url' }, { dataType: 'patch' });
expect(store.modifyResource).toHaveBeenLastCalledWith({ path: 'url' }, { binary: false });
});
});

View File

@@ -11,15 +11,15 @@ describe('A PostOperationHandler', (): void => {
const handler = new PostOperationHandler(store);
it('only supports POST operations with a body.', async(): Promise<void> => {
await expect(handler.canHandle({ method: 'POST', body: { dataType: 'test' }} as Operation))
await expect(handler.canHandle({ method: 'POST', body: { }} as Operation))
.resolves.toBeUndefined();
await expect(handler.canHandle({ method: 'GET', body: { dataType: 'test' }} as Operation))
await expect(handler.canHandle({ method: 'GET', body: { }} as Operation))
.rejects.toThrow(UnsupportedHttpError);
await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
});
it('adds the given representation to the store and returns the new identifier.', async(): Promise<void> => {
await expect(handler.handle({ method: 'POST', body: { dataType: 'test' }} as Operation))
await expect(handler.handle({ method: 'POST', body: { }} as Operation))
.resolves.toEqual({ identifier: { path: 'newPath' }});
});
});

View File

@@ -17,10 +17,10 @@ describe('A PutOperationHandler', (): void => {
});
it('sets the representation in the store and returns its identifier.', async(): Promise<void> => {
await expect(handler.handle({ target: { path: 'url' }, body: { dataType: 'test' }} as Operation))
await expect(handler.handle({ target: { path: 'url' }, body: {}} as Operation))
.resolves.toEqual({ identifier: { path: 'url' }});
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
expect(store.setRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, { dataType: 'test' });
expect(store.setRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, {});
});
it('errors when there is no body.', async(): Promise<void> => {

View File

@@ -6,10 +6,10 @@ import arrayifyStream from 'arrayify-stream';
import { DataFactory } from 'n3';
import streamifyArray from 'streamify-array';
import { RuntimeConfig } from '../../../src/init/RuntimeConfig';
import { BinaryRepresentation } from '../../../src/ldp/representation/BinaryRepresentation';
import { Representation } from '../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata';
import { FileResourceStore } from '../../../src/storage/FileResourceStore';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../src/util/ContentTypes';
import { CONTENT_TYPE_QUADS } from '../../../src/util/ContentTypes';
import { ConflictHttpError } from '../../../src/util/errors/ConflictHttpError';
import { MethodNotAllowedHttpError } from '../../../src/util/errors/MethodNotAllowedHttpError';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
@@ -33,7 +33,7 @@ fsPromises.access = jest.fn();
describe('A FileResourceStore', (): void => {
let store: FileResourceStore;
let representation: BinaryRepresentation;
let representation: Representation;
let readableMock: Readable;
let stats: Stats;
let writeStream: WriteStream;
@@ -56,8 +56,8 @@ describe('A FileResourceStore', (): void => {
);
representation = {
binary: true,
data: streamifyArray([ rawData ]),
dataType: DATA_TYPE_BINARY,
metadata: { raw: [], linkRel: { type: new Set() }} as RepresentationMetadata,
};
@@ -116,7 +116,7 @@ describe('A FileResourceStore', (): void => {
});
it('errors for wrong input data types.', async(): Promise<void> => {
(representation as any).dataType = DATA_TYPE_QUAD;
(representation as any).binary = false;
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
await expect(store.setRepresentation({ path: `${base}foo` }, representation)).rejects
.toThrow(UnsupportedMediaTypeHttpError);
@@ -142,7 +142,7 @@ describe('A FileResourceStore', (): void => {
// Read container
const result = await store.getRepresentation(identifier);
expect(result).toEqual({
dataType: DATA_TYPE_QUAD,
binary: false,
data: expect.any(Readable),
metadata: {
raw: [],
@@ -212,7 +212,7 @@ describe('A FileResourceStore', (): void => {
expect(fs.createWriteStream as jest.Mock).toBeCalledWith(joinPath(rootFilepath, 'file.txt'));
const result = await store.getRepresentation({ path: `${base}file.txt` });
expect(result).toEqual({
dataType: DATA_TYPE_BINARY,
binary: true,
data: expect.any(Readable),
metadata: {
raw: [],
@@ -365,7 +365,7 @@ describe('A FileResourceStore', (): void => {
];
const result = await store.getRepresentation({ path: `${base}foo/` });
expect(result).toEqual({
dataType: DATA_TYPE_QUAD,
binary: false,
data: expect.any(Readable),
metadata: {
raw: [],
@@ -492,7 +492,7 @@ describe('A FileResourceStore', (): void => {
const result = await store.getRepresentation({ path: `${base}.htaccess` });
expect(result).toEqual({
dataType: DATA_TYPE_BINARY,
binary: true,
data: expect.any(Readable),
metadata: {
raw: [],

View File

@@ -2,25 +2,24 @@ import { Readable } from 'stream';
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { RuntimeConfig } from '../../../src/init/RuntimeConfig';
import { BinaryRepresentation } from '../../../src/ldp/representation/BinaryRepresentation';
import { Representation } from '../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata';
import { InMemoryResourceStore } from '../../../src/storage/InMemoryResourceStore';
import { DATA_TYPE_BINARY } from '../../../src/util/ContentTypes';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
const base = 'http://test.com/';
describe('A InMemoryResourceStore', (): void => {
let store: InMemoryResourceStore;
let representation: BinaryRepresentation;
let representation: Representation;
const dataString = '<http://test.com/s> <http://test.com/p> <http://test.com/o>.';
beforeEach(async(): Promise<void> => {
store = new InMemoryResourceStore(new RuntimeConfig({ base }));
representation = {
binary: true,
data: streamifyArray([ dataString ]),
dataType: DATA_TYPE_BINARY,
metadata: {} as RepresentationMetadata,
};
});
@@ -43,7 +42,7 @@ describe('A InMemoryResourceStore', (): void => {
expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier);
expect(result).toEqual({
dataType: representation.dataType,
binary: true,
data: expect.any(Readable),
metadata: representation.metadata,
});
@@ -61,7 +60,7 @@ describe('A InMemoryResourceStore', (): void => {
await store.setRepresentation({ path: base }, representation);
const result = await store.getRepresentation({ path: base });
expect(result).toEqual({
dataType: representation.dataType,
binary: true,
data: expect.any(Readable),
metadata: representation.metadata,
});

View File

@@ -6,7 +6,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes';
describe('A QuadToRdfConverter', (): void => {
const converter = new QuadToRdfConverter();
@@ -44,7 +44,7 @@ describe('A QuadToRdfConverter', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toMatchObject({
dataType: DATA_TYPE_BINARY,
binary: true,
metadata: {
contentType: 'text/turtle',
},
@@ -67,7 +67,7 @@ describe('A QuadToRdfConverter', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toMatchObject({
dataType: DATA_TYPE_BINARY,
binary: true,
metadata: {
contentType: 'application/ld+json',
},

View File

@@ -5,7 +5,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { QuadToTurtleConverter } from '../../../../src/storage/conversion/QuadToTurtleConverter';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes';
describe('A QuadToTurtleConverter', (): void => {
const converter = new QuadToTurtleConverter();
@@ -29,7 +29,7 @@ describe('A QuadToTurtleConverter', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toMatchObject({
dataType: DATA_TYPE_BINARY,
binary: true,
metadata: {
contentType: 'text/turtle',
},

View File

@@ -7,7 +7,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { RdfToQuadConverter } from '../../../../src/storage/conversion/RdfToQuadConverter';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A RdfToQuadConverter.test.ts', (): void => {
@@ -42,8 +42,8 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},
@@ -63,8 +63,8 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},
@@ -84,8 +84,8 @@ describe('A RdfToQuadConverter.test.ts', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},

View File

@@ -6,7 +6,7 @@ import { Representation } from '../../../../src/ldp/representation/Representatio
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { TurtleToQuadConverter } from '../../../../src/storage/conversion/TurtleToQuadConverter';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A TurtleToQuadConverter', (): void => {
@@ -27,8 +27,8 @@ describe('A TurtleToQuadConverter', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},
@@ -48,8 +48,8 @@ describe('A TurtleToQuadConverter', (): void => {
const preferences: RepresentationPreferences = { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]};
const result = await converter.handle({ identifier, representation, preferences });
expect(result).toEqual({
binary: false,
data: expect.any(Readable),
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: CONTENT_TYPE_QUADS,
},

View File

@@ -8,7 +8,7 @@ import { Lock } from '../../../../src/storage/Lock';
import { SparqlUpdatePatchHandler } from '../../../../src/storage/patch/SparqlUpdatePatchHandler';
import { ResourceLocker } from '../../../../src/storage/ResourceLocker';
import { ResourceStore } from '../../../../src/storage/ResourceStore';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { CONTENT_TYPE_QUADS } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A SparqlUpdatePatchHandler', (): void => {
@@ -72,7 +72,7 @@ describe('A SparqlUpdatePatchHandler', (): void => {
const setParams = (source.setRepresentation as jest.Mock).mock.calls[0];
expect(setParams[0]).toEqual({ path: 'path' });
expect(setParams[1]).toEqual(expect.objectContaining({
dataType: DATA_TYPE_QUAD,
binary: false,
metadata: { raw: [], profiles: [], contentType: CONTENT_TYPE_QUADS },
}));
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);