feat: Patch containers by recreating Representation from metadata

Also included is a change to the Patching architecture.
Patching is now done by RepresentationPatchers that take a Representation as input.
This commit is contained in:
Joachim Van Herwegen
2021-09-13 15:32:15 +02:00
parent a1c3633a25
commit ef9703e284
18 changed files with 804 additions and 483 deletions

View File

@@ -0,0 +1,63 @@
import type { Patch } from '../../../../src/ldp/http/Patch';
import { BasicRepresentation } from '../../../../src/ldp/representation/BasicRepresentation';
import type { PatchHandlerInput } from '../../../../src/storage/patch/PatchHandler';
import type { RepresentationPatcher } from '../../../../src/storage/patch/RepresentationPatcher';
import { RepresentationPatchHandler } from '../../../../src/storage/patch/RepresentationPatchHandler';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
describe('A RepresentationPatchHandler', (): void => {
const identifier = { path: 'http://test.com/foo' };
const representation = new BasicRepresentation('', 'text/turtle');
const patch: Patch = new BasicRepresentation('', 'application/sparql-update');
const patchResult = new BasicRepresentation('', 'application/trig');
let input: PatchHandlerInput;
let source: jest.Mocked<ResourceStore>;
let patcher: jest.Mocked<RepresentationPatcher>;
let handler: RepresentationPatchHandler;
beforeEach(async(): Promise<void> => {
source = {
getRepresentation: jest.fn().mockResolvedValue(representation),
setRepresentation: jest.fn().mockResolvedValue([ identifier ]),
} as any;
input = { source, identifier, patch };
patcher = {
handleSafe: jest.fn().mockResolvedValue(patchResult),
} as any;
handler = new RepresentationPatchHandler(patcher);
});
it('calls the patcher with the representation from the store.', async(): Promise<void> => {
await expect(handler.handle(input)).resolves.toEqual([ identifier ]);
expect(patcher.handleSafe).toHaveBeenCalledTimes(1);
expect(patcher.handleSafe).toHaveBeenLastCalledWith({ identifier, patch, representation });
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(source.setRepresentation).toHaveBeenLastCalledWith(identifier, patchResult);
});
it('calls the patcher with no representation if there is none.', async(): Promise<void> => {
source.getRepresentation.mockRejectedValueOnce(new NotFoundHttpError());
await expect(handler.handle(input)).resolves.toEqual([ identifier ]);
expect(patcher.handleSafe).toHaveBeenCalledTimes(1);
expect(patcher.handleSafe).toHaveBeenLastCalledWith({ identifier, patch });
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(source.setRepresentation).toHaveBeenLastCalledWith(identifier, patchResult);
});
it('errors if the store throws a non-404 error.', async(): Promise<void> => {
const error = new BadRequestHttpError();
source.getRepresentation.mockRejectedValueOnce(error);
await expect(handler.handle(input)).rejects.toThrow(error);
});
});