refactor: Rename resourceExists to hasResource

The function was also moved to the smaller interface ResourceSet.
This commit is contained in:
Joachim Van Herwegen
2022-02-23 15:41:31 +01:00
parent 2ae5924dde
commit 4404fa07d9
26 changed files with 73 additions and 64 deletions

View File

@@ -19,7 +19,7 @@ describe('A PatchOperationHandler', (): void => {
operation = { method: 'PATCH', target: { path: 'http://test.com/foo' }, body, conditions, preferences: {}};
store = {
resourceExists: jest.fn(),
hasResource: jest.fn(),
modifyResource: jest.fn(),
} as any;
@@ -47,7 +47,7 @@ describe('A PatchOperationHandler', (): void => {
});
it('returns the correct response if the resource already exists.', async(): Promise<void> => {
store.resourceExists.mockResolvedValueOnce(true);
store.hasResource.mockResolvedValueOnce(true);
const result = await handler.handle({ operation });
expect(store.modifyResource).toHaveBeenCalledTimes(1);
expect(store.modifyResource).toHaveBeenLastCalledWith(operation.target, body, conditions);

View File

@@ -18,7 +18,7 @@ describe('A PutOperationHandler', (): void => {
body = new BasicRepresentation('', 'text/turtle');
operation = { method: 'PUT', target: { path: 'http://test.com/foo' }, body, conditions, preferences: {}};
store = {
resourceExists: jest.fn(),
hasResource: jest.fn(),
setRepresentation: jest.fn(),
} as any;
@@ -46,7 +46,7 @@ describe('A PutOperationHandler', (): void => {
});
it('returns the correct response if the resource already exists.', async(): Promise<void> => {
store.resourceExists.mockResolvedValueOnce(true);
store.hasResource.mockResolvedValueOnce(true);
const result = await handler.handle({ operation });
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
expect(store.setRepresentation).toHaveBeenLastCalledWith(operation.target, body, conditions);

View File

@@ -20,7 +20,7 @@ describe('A GeneratedPodManager', (): void => {
};
store = {
setRepresentation: jest.fn(),
resourceExists: jest.fn(),
hasResource: jest.fn(),
} as any;
generatorData = [
{ identifier: { path: '/path/' }, representation: '/' as any },
@@ -36,7 +36,7 @@ describe('A GeneratedPodManager', (): void => {
});
it('throws an error if the generate identifier is not available.', async(): Promise<void> => {
store.resourceExists.mockResolvedValueOnce(true);
store.hasResource.mockResolvedValueOnce(true);
const result = manager.createPod({ path: `${base}user/` }, settings, false);
await expect(result).rejects.toThrow(`There already is a resource at ${base}user/`);
await expect(result).rejects.toThrow(ConflictHttpError);
@@ -52,7 +52,7 @@ describe('A GeneratedPodManager', (): void => {
});
it('allows overwriting when enabled.', async(): Promise<void> => {
store.resourceExists.mockResolvedValueOnce(true);
store.hasResource.mockResolvedValueOnce(true);
await expect(manager.createPod({ path: `${base}${settings.login}/` }, settings, true)).resolves.toBeUndefined();
expect(store.setRepresentation).toHaveBeenCalledTimes(3);

View File

@@ -25,7 +25,7 @@ describe('A BaseResourceStore', (): void => {
await expect(store.modifyResource(any, any)).rejects.toThrow(NotImplementedHttpError);
});
it('errors on resourceExists.', async(): Promise<void> => {
await expect(store.resourceExists(any)).rejects.toThrow(NotImplementedHttpError);
it('errors on hasResource.', async(): Promise<void> => {
await expect(store.hasResource(any)).rejects.toThrow(NotImplementedHttpError);
});
});

View File

@@ -726,13 +726,13 @@ describe('A DataAccessorBasedStore', (): void => {
describe('resource Exists', (): void => {
it('should return false when the resource does not exist.', async(): Promise<void> => {
const resourceID = { path: `${root}resource` };
await expect(store.resourceExists(resourceID)).resolves.toBeFalsy();
await expect(store.hasResource(resourceID)).resolves.toBeFalsy();
});
it('should return true when the resource exists.', async(): Promise<void> => {
const resourceID = { path: `${root}resource` };
accessor.data[resourceID.path] = representation;
await expect(store.resourceExists(resourceID)).resolves.toBeTruthy();
await expect(store.hasResource(resourceID)).resolves.toBeTruthy();
});
it('should rethrow any unexpected errors from validateIdentifier.', async(): Promise<void> => {
@@ -741,7 +741,7 @@ describe('A DataAccessorBasedStore', (): void => {
accessor.getMetadata = jest.fn(async(): Promise<any> => {
throw new Error('error');
});
await expect(store.resourceExists(resourceID)).rejects.toThrow('error');
await expect(store.hasResource(resourceID)).rejects.toThrow('error');
accessor.getMetadata = originalMetaData;
});
});

View File

@@ -39,7 +39,7 @@ describe('A LockingResourceStore', (): void => {
setRepresentation: jest.fn((): any => addOrder('setRepresentation')),
deleteResource: jest.fn((): any => addOrder('deleteResource')),
modifyResource: jest.fn((): any => addOrder('modifyResource')),
resourceExists: jest.fn((): any => addOrder('resourceExists')),
hasResource: jest.fn((): any => addOrder('hasResource')),
};
timeoutTrigger = new EventEmitter();
@@ -287,12 +287,12 @@ describe('A LockingResourceStore', (): void => {
expect(order).toEqual([ 'lock read', 'useless get', 'timeout', 'unlock read' ]);
});
it('resourceExists should only acquire and release the read lock.', async(): Promise<void> => {
await store.resourceExists(subjectId);
it('hasResource should only acquire and release the read lock.', async(): Promise<void> => {
await store.hasResource(subjectId);
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect(locker.withWriteLock).toHaveBeenCalledTimes(0);
expect(source.resourceExists).toHaveBeenCalledTimes(1);
expect(source.resourceExists).toHaveBeenLastCalledWith(subjectId, undefined);
expect(order).toEqual([ 'lock read', 'resourceExists', 'unlock read' ]);
expect(source.hasResource).toHaveBeenCalledTimes(1);
expect(source.hasResource).toHaveBeenLastCalledWith(subjectId);
expect(order).toEqual([ 'lock read', 'hasResource', 'unlock read' ]);
});
});

View File

@@ -19,7 +19,7 @@ describe('A MonitoringStore', (): void => {
setRepresentation: jest.fn(async(): Promise<any> => modified),
deleteResource: jest.fn(async(): Promise<any> => modified),
modifyResource: jest.fn(async(): Promise<any> => modified),
resourceExists: jest.fn(async(): Promise<any> => undefined),
hasResource: jest.fn(async(): Promise<any> => undefined),
};
store = new MonitoringStore(source);
changedCallback = jest.fn();
@@ -106,9 +106,9 @@ describe('A MonitoringStore', (): void => {
expect(changedCallback).toHaveBeenCalledWith({ path: 'http://example.org/modified/2' });
});
it('calls resourceExists directly from the source.', async(): Promise<void> => {
await expect(store.resourceExists({ path: 'http://example.org/foo/bar' })).resolves.toBeUndefined();
expect(source.resourceExists).toHaveBeenCalledTimes(1);
expect(source.resourceExists).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' }, undefined);
it('calls hasResource directly from the source.', async(): Promise<void> => {
await expect(store.hasResource({ path: 'http://example.org/foo/bar' })).resolves.toBeUndefined();
expect(source.hasResource).toHaveBeenCalledTimes(1);
expect(source.hasResource).toHaveBeenLastCalledWith({ path: 'http://example.org/foo/bar' });
});
});

View File

@@ -14,7 +14,7 @@ describe('A PassthroughStore', (): void => {
setRepresentation: jest.fn(async(): Promise<any> => 'set'),
deleteResource: jest.fn(async(): Promise<any> => 'delete'),
modifyResource: jest.fn(async(): Promise<any> => 'modify'),
resourceExists: jest.fn(async(): Promise<any> => 'exists'),
hasResource: jest.fn(async(): Promise<any> => 'exists'),
};
store = new PassthroughStore(source);
@@ -50,9 +50,9 @@ describe('A PassthroughStore', (): void => {
expect(source.modifyResource).toHaveBeenLastCalledWith({ path: 'modifyPath' }, {}, undefined);
});
it('calls resourceExists directly from the source.', async(): Promise<void> => {
await expect(store.resourceExists({ path: 'existsPath' })).resolves.toBe('exists');
expect(source.resourceExists).toHaveBeenCalledTimes(1);
expect(source.resourceExists).toHaveBeenLastCalledWith({ path: 'existsPath' }, undefined);
it('calls hasResource directly from the source.', async(): Promise<void> => {
await expect(store.hasResource({ path: 'existsPath' })).resolves.toBe('exists');
expect(source.hasResource).toHaveBeenCalledTimes(1);
expect(source.hasResource).toHaveBeenLastCalledWith({ path: 'existsPath' });
});
});

View File

@@ -17,7 +17,7 @@ describe('A RoutingResourceStore', (): void => {
setRepresentation: jest.fn(),
modifyResource: jest.fn(),
deleteResource: jest.fn(),
resourceExists: jest.fn(),
hasResource: jest.fn(),
};
rule = new StaticAsyncHandler(true, source);
@@ -60,10 +60,10 @@ describe('A RoutingResourceStore', (): void => {
expect(source.deleteResource).toHaveBeenLastCalledWith(identifier, 'conditions');
});
it('calls resourceExists on the resulting store.', async(): Promise<void> => {
await expect(store.resourceExists(identifier)).resolves.toBeUndefined();
expect(source.resourceExists).toHaveBeenCalledTimes(1);
expect(source.resourceExists).toHaveBeenLastCalledWith(identifier, undefined);
it('calls hasResource on the resulting store.', async(): Promise<void> => {
await expect(store.hasResource(identifier)).resolves.toBeUndefined();
expect(source.hasResource).toHaveBeenCalledTimes(1);
expect(source.hasResource).toHaveBeenLastCalledWith(identifier);
});
it('throws a 404 if there is no body and no store was found.', async(): Promise<void> => {

View File

@@ -19,7 +19,7 @@ describe('A JsonResourceStorage', (): void => {
beforeEach(async(): Promise<void> => {
const data: Record<string, string> = { };
store = {
async resourceExists(identifier: ResourceIdentifier): Promise<boolean> {
async hasResource(identifier: ResourceIdentifier): Promise<boolean> {
return Boolean(data[identifier.path]);
},
async getRepresentation(identifier: ResourceIdentifier): Promise<Representation> {

View File

@@ -39,18 +39,18 @@ describe('A ConvertingRouterRule', (): void => {
});
it('checks if the stores contain the identifier if there is no data.', async(): Promise<void> => {
store1.resourceExists = jest.fn().mockImplementationOnce((): any => true);
store1.hasResource = jest.fn().mockImplementationOnce((): any => true);
await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(store1);
expect(store1.resourceExists).toHaveBeenCalledTimes(1);
expect(store1.hasResource).toHaveBeenCalledTimes(1);
});
it('returns the defaultStore if no other store has the resource.', async(): Promise<void> => {
store1.resourceExists = jest.fn().mockImplementationOnce((): any => false);
store1.hasResource = jest.fn().mockImplementationOnce((): any => false);
await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(defaultStore);
});
it('throws the error if a store had a non-404 error.', async(): Promise<void> => {
store1.resourceExists = jest.fn().mockRejectedValueOnce(new InternalServerError());
store1.hasResource = jest.fn().mockRejectedValueOnce(new InternalServerError());
await expect(rule.handle({ identifier: { path: 'identifier' }})).rejects.toThrow(InternalServerError);
});
});