diff --git a/src/storage/accessors/FileDataAccessor.ts b/src/storage/accessors/FileDataAccessor.ts new file mode 100644 index 000000000..26da8c6ec --- /dev/null +++ b/src/storage/accessors/FileDataAccessor.ts @@ -0,0 +1,331 @@ +import type { Stats } from 'fs'; +import { createWriteStream, createReadStream, promises as fsPromises } from 'fs'; +import { posix } from 'path'; +import type { Readable } from 'stream'; +import { DataFactory } from 'n3'; +import type { NamedNode, Quad } from 'rdf-js'; +import type { Representation } from '../../ldp/representation/Representation'; +import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata'; +import type { ResourceIdentifier } from '../../ldp/representation/ResourceIdentifier'; +import { ConflictHttpError } from '../../util/errors/ConflictHttpError'; +import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError'; +import { isSystemError } from '../../util/errors/SystemError'; +import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError'; +import type { MetadataController } from '../../util/MetadataController'; +import { CONTENT_TYPE, DCTERMS, POSIX, RDF, XSD } from '../../util/UriConstants'; +import { toNamedNode, toTypedLiteral } from '../../util/UriUtil'; +import { pushQuad } from '../../util/Util'; +import type { ExtensionBasedMapper } from '../ExtensionBasedMapper'; +import type { ResourceLink } from '../FileIdentifierMapper'; +import type { DataAccessor } from './DataAccessor'; + +const { join: joinPath } = posix; + +/** + * DataAccessor that uses the file system to store data resources as files and containers as folders. + */ +export class FileDataAccessor implements DataAccessor { + private readonly resourceMapper: ExtensionBasedMapper; + private readonly metadataController: MetadataController; + + public constructor(resourceMapper: ExtensionBasedMapper, metadataController: MetadataController) { + this.resourceMapper = resourceMapper; + this.metadataController = metadataController; + } + + /** + * Only binary data can be directly stored as files so will error on non-binary data. + */ + public async canHandle(representation: Representation): Promise { + if (!representation.binary) { + throw new UnsupportedMediaTypeHttpError('Only binary data is supported.'); + } + } + + /** + * Will return data stream directly to the file corresponding to the resource. + * Will throw NotFoundHttpError if the input is a container. + */ + public async getData(identifier: ResourceIdentifier): Promise { + const link = await this.resourceMapper.mapUrlToFilePath(identifier); + const stats = await this.getStats(link.filePath); + + if (stats.isFile()) { + return createReadStream(link.filePath); + } + + throw new NotFoundHttpError(); + } + + /** + * Will return corresponding metadata by reading the metadata file (if it exists) + * and adding file system specific metadata elements. + */ + public async getMetadata(identifier: ResourceIdentifier): Promise { + const link = await this.resourceMapper.mapUrlToFilePath(identifier); + const stats = await this.getStats(link.filePath); + if (!identifier.path.endsWith('/') && stats.isFile()) { + return this.getFileMetadata(link, stats); + } + if (identifier.path.endsWith('/') && stats.isDirectory()) { + return this.getDirectoryMetadata(link, stats); + } + throw new NotFoundHttpError(); + } + + /** + * Writes the given data as a file (and potential metadata as additional file). + * The metadata file will be written first and will be deleted if something goes wrong writing the actual data. + */ + public async writeDocument(identifier: ResourceIdentifier, data: Readable, metadata: RepresentationMetadata): + Promise { + const link = await this.resourceMapper + .mapUrlToFilePath(identifier, metadata.contentType); + if (this.isMetadataPath(link.filePath)) { + throw new ConflictHttpError('Not allowed to create files with the metadata extension.'); + } + + const wroteMetadata = await this.writeMetadata(link, metadata); + + try { + await this.writeDataFile(link.filePath, data); + } catch (error: unknown) { + // Delete the metadata if there was an error writing the file + if (wroteMetadata) { + await fsPromises.unlink(this.getMetadataPath(link.filePath)); + } + throw error; + } + } + + /** + * Creates corresponding folder if necessary and writes metadata to metadata file if necessary. + */ + public async writeContainer(identifier: ResourceIdentifier, metadata: RepresentationMetadata): Promise { + const link = await this.resourceMapper.mapUrlToFilePath(identifier); + try { + await fsPromises.mkdir(link.filePath); + } catch (error: unknown) { + // Don't throw if directory already exists + if (!isSystemError(error) || error.code !== 'EEXIST') { + throw error; + } + } + + await this.writeMetadata(link, metadata); + } + + /** + * Removes the corresponding file/folder (and metadata file). + */ + public async deleteResource(identifier: ResourceIdentifier): Promise { + const link = await this.resourceMapper.mapUrlToFilePath(identifier); + const stats = await this.getStats(link.filePath); + + try { + await fsPromises.unlink(this.getMetadataPath(link.filePath)); + } catch (error: unknown) { + // Ignore if it doesn't exist + if (!isSystemError(error) || error.code !== 'ENOENT') { + throw error; + } + } + + if (!identifier.path.endsWith('/') && stats.isFile()) { + await fsPromises.unlink(link.filePath); + } else if (identifier.path.endsWith('/') && stats.isDirectory()) { + await fsPromises.rmdir(link.filePath); + } else { + throw new NotFoundHttpError(); + } + } + + /** + * Gets the Stats object corresponding to the given file path. + * @param path - File path to get info from. + * + * @throws NotFoundHttpError + * If the file/folder doesn't exist. + */ + private async getStats(path: string): Promise { + try { + return await fsPromises.lstat(path); + } catch (error: unknown) { + if (isSystemError(error) && error.code === 'ENOENT') { + throw new NotFoundHttpError(); + } + throw error; + } + } + + /** + * Generates file path that corresponds to the metadata file of the given file path. + */ + private getMetadataPath(path: string): string { + return `${path}.meta`; + } + + /** + * Checks if the given file path is a metadata path. + */ + private isMetadataPath(path: string): boolean { + return path.endsWith('.meta'); + } + + /** + * Reads and generates all metadata relevant for the given file, + * ingesting it into a RepresentationMetadata object. + * + * @param link - Path related metadata. + * @param stats - Stats object of the corresponding file. + */ + private async getFileMetadata(link: ResourceLink, stats: Stats): + Promise { + return (await this.getBaseMetadata(link, stats, false)) + .set(CONTENT_TYPE, link.contentType); + } + + /** + * Reads and generates all metadata relevant for the given directory, + * ingesting it into a RepresentationMetadata object. + * + * @param link - Path related metadata. + * @param stats - Stats object of the corresponding directory. + */ + private async getDirectoryMetadata(link: ResourceLink, stats: Stats): + Promise { + return (await this.getBaseMetadata(link, stats, true)) + .addQuads(await this.getChildMetadataQuads(link)); + } + + /** + * Writes the metadata of the resource to a meta file. + * @param link - Path related metadata of the resource. + * @param metadata - Metadata to write. + * + * @returns True if data was written to a file. + */ + private async writeMetadata(link: ResourceLink, metadata: RepresentationMetadata): Promise { + // These are stored by file system conventions + metadata.removeAll(RDF.type); + metadata.removeAll(CONTENT_TYPE); + const quads = metadata.quads(); + if (quads.length > 0) { + const serializedMetadata = this.metadataController.serializeQuads(quads); + await this.writeDataFile(this.getMetadataPath(link.filePath), serializedMetadata); + return true; + } + return false; + } + + /** + * Generates metadata relevant for any resources stored by this accessor. + * @param link - Path related metadata. + * @param stats - Stats objects of the corresponding directory. + * @param isContainer - If the path points to a container (directory) or not. + */ + private async getBaseMetadata(link: ResourceLink, stats: Stats, isContainer: boolean): + Promise { + const metadata = new RepresentationMetadata(link.identifier.path) + .addQuads(await this.getRawMetadata(link.filePath)); + metadata.addQuads(this.metadataController.generateResourceQuads(metadata.identifier as NamedNode, isContainer)); + metadata.addQuads(this.generatePosixQuads(metadata.identifier as NamedNode, stats)); + return metadata; + } + + /** + * Reads the metadata from the corresponding metadata file. + * Returns an empty array if there is no metadata file. + * + * @param path - File path of the resource (not the metadata!). + */ + private async getRawMetadata(path: string): Promise { + try { + // Check if the metadata file exists first + await fsPromises.lstat(this.getMetadataPath(path)); + + const readMetadataStream = createReadStream(this.getMetadataPath(path)); + return await this.metadataController.parseQuads(readMetadataStream); + } catch (error: unknown) { + // Metadata file doesn't exist so lets keep `rawMetaData` an empty array. + if (!isSystemError(error) || error.code !== 'ENOENT') { + throw error; + } + return []; + } + } + + /** + * Generate all containment related triples for a container. + * These include the actual containment triples and specific triples for every child resource. + * + * @param link - Path related metadata. + */ + private async getChildMetadataQuads(link: ResourceLink): Promise { + const quads: Quad[] = []; + const childURIs: string[] = []; + const files = await fsPromises.readdir(link.filePath); + + // For every child in the container we want to generate specific metadata + for (const childName of files) { + // Hide metadata files from containment triples + if (this.isMetadataPath(childName)) { + continue; + } + + // Ignore non-file/directory entries in the folder + const childStats = await fsPromises.lstat(joinPath(link.filePath, childName)); + if (!childStats.isFile() && !childStats.isDirectory()) { + continue; + } + + // Generate the URI corresponding to the child resource + const childLink = await this.resourceMapper + .mapFilePathToUrl(joinPath(link.filePath, childName), childStats.isDirectory()); + + // Generate metadata of this specific child + const subject = DataFactory.namedNode(childLink.identifier.path); + quads.push(...this.metadataController.generateResourceQuads(subject, childStats.isDirectory())); + quads.push(...this.generatePosixQuads(subject, childStats)); + childURIs.push(childLink.identifier.path); + } + + // Generate containment metadata + const containsQuads = this.metadataController.generateContainerContainsResourceQuads( + DataFactory.namedNode(link.identifier.path), childURIs, + ); + + return quads.concat(containsQuads); + } + + /** + * Helper function to add file system related metadata. + * @param subject - Subject for the new quads. + * @param stats - Stats of the file/directory corresponding to the resource. + */ + private generatePosixQuads(subject: NamedNode, stats: Stats): Quad[] { + const quads: Quad[] = []; + pushQuad(quads, subject, toNamedNode(POSIX.size), toTypedLiteral(stats.size, XSD.integer)); + pushQuad(quads, subject, toNamedNode(DCTERMS.modified), toTypedLiteral(stats.mtime.toISOString(), XSD.dateTime)); + pushQuad(quads, subject, toNamedNode(POSIX.mtime), toTypedLiteral( + Math.floor(stats.mtime.getTime() / 1000), XSD.integer, + )); + return quads; + } + + /** + * Helper function without extra validation checking to create a data file. + * @param path - The filepath of the file to be created. + * @param data - The data to be put in the file. + */ + private async writeDataFile(path: string, data: Readable): Promise { + return new Promise((resolve, reject): any => { + const writeStream = createWriteStream(path); + data.pipe(writeStream); + data.on('error', reject); + + writeStream.on('error', reject); + writeStream.on('finish', resolve); + }); + } +} diff --git a/test/unit/storage/accessors/FileDataAccessor.test.ts b/test/unit/storage/accessors/FileDataAccessor.test.ts new file mode 100644 index 000000000..30929406e --- /dev/null +++ b/test/unit/storage/accessors/FileDataAccessor.test.ts @@ -0,0 +1,291 @@ +import streamifyArray from 'streamify-array'; +import type { Representation } from '../../../../src/ldp/representation/Representation'; +import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata'; +import { FileDataAccessor } from '../../../../src/storage/accessors/FileDataAccessor'; +import { ExtensionBasedMapper } from '../../../../src/storage/ExtensionBasedMapper'; +import { APPLICATION_OCTET_STREAM } from '../../../../src/util/ContentTypes'; +import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError'; +import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError'; +import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; +import { MetadataController } from '../../../../src/util/MetadataController'; +import { CONTENT_TYPE, DCTERMS, LDP, POSIX, RDF, XSD } from '../../../../src/util/UriConstants'; +import { toNamedNode, toTypedLiteral } from '../../../../src/util/UriUtil'; +import { readableToString } from '../../../../src/util/Util'; +import { mockFs } from '../../../util/Util'; + +jest.mock('fs'); + +const rootFilePath = 'uploads'; +const now = new Date(); + +describe('A FileDataAccessor', (): void => { + const base = 'http://test.com/'; + let accessor: FileDataAccessor; + let cache: { data: any }; + let metadata: RepresentationMetadata; + + beforeEach(async(): Promise => { + cache = mockFs(rootFilePath, now); + accessor = new FileDataAccessor( + new ExtensionBasedMapper(base, rootFilePath), + new MetadataController(), + ); + + metadata = new RepresentationMetadata({ [CONTENT_TYPE]: APPLICATION_OCTET_STREAM }); + }); + + it('can only handle binary data.', async(): Promise => { + await expect(accessor.canHandle({ binary: true } as Representation)).resolves.toBeUndefined(); + await expect(accessor.canHandle({ binary: false } as Representation)).rejects + .toThrow(new UnsupportedMediaTypeHttpError('Only binary data is supported.')); + }); + + describe('getting data', (): void => { + it('throws a 404 if the identifier does not start with the base.', async(): Promise => { + await expect(accessor.getData({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if the identifier does not match an existing file.', async(): Promise => { + await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if the identifier matches a directory.', async(): Promise => { + cache.data = { resource: {}}; + await expect(accessor.getData({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); + }); + + it('returns the corresponding data.', async(): Promise => { + cache.data = { resource: 'data' }; + const stream = await accessor.getData({ path: `${base}resource` }); + await expect(readableToString(stream)).resolves.toBe('data'); + }); + }); + + describe('getting metadata', (): void => { + it('throws a 404 if the identifier does not start with the base.', async(): Promise => { + await expect(accessor.getMetadata({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if the identifier does not match an existing file.', async(): Promise => { + await expect(accessor.getMetadata({ path: `${base}container/` })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if it matches something that is no file or directory.', async(): Promise => { + cache.data = { resource: 5 }; + await expect(accessor.getMetadata({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws an error if something else went wrong.', async(): Promise => { + jest.requireMock('fs').promises.lstat = (): any => { + throw new Error('error'); + }; + await expect(accessor.getMetadata({ path: base })).rejects.toThrow(new Error('error')); + }); + + it('throws a 404 if the trailing slash does not match its type.', async(): Promise => { + cache.data = { resource: 'data' }; + await expect(accessor.getMetadata({ path: `${base}resource/` })).rejects.toThrow(NotFoundHttpError); + cache.data = { container: {}}; + await expect(accessor.getMetadata({ path: `${base}container` })).rejects.toThrow(NotFoundHttpError); + }); + + it('generates the metadata for a resource.', async(): Promise => { + cache.data = { 'resource.ttl': 'data' }; + metadata = await accessor.getMetadata({ path: `${base}resource.ttl` }); + expect(metadata.identifier.value).toBe(`${base}resource.ttl`); + expect(metadata.contentType).toBe('text/turtle'); + expect(metadata.get(RDF.type)?.value).toBe(LDP.Resource); + expect(metadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral('data'.length, XSD.integer)); + expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime)); + expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer)); + }); + + it('generates the metadata for a container and its non-meta children.', async(): Promise => { + cache.data = { container: { resource: 'data', 'resource.meta': 'metadata', notAFile: 5, container2: {}}}; + metadata = await accessor.getMetadata({ path: `${base}container/` }); + expect(metadata.identifier.value).toBe(`${base}container/`); + expect(metadata.getAll(RDF.type)).toEqualRdfTermArray( + [ toNamedNode(LDP.Container), toNamedNode(LDP.BasicContainer), toNamedNode(LDP.Resource) ], + ); + expect(metadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral(0, XSD.integer)); + expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime)); + expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer)); + expect(metadata.getAll(LDP.contains)).toEqualRdfTermArray( + [ toNamedNode(`${base}container/resource`), toNamedNode(`${base}container/container2/`) ], + ); + + const childQuads = metadata.quads().filter((quad): boolean => + quad.subject.value === `${base}container/resource`); + const childMetadata = new RepresentationMetadata(`${base}container/resource`).addQuads(childQuads); + expect(childMetadata.get(RDF.type)?.value).toBe(LDP.Resource); + expect(childMetadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral('data'.length, XSD.integer)); + expect(childMetadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime)); + expect(childMetadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), + XSD.integer)); + }); + + it('adds stored metadata when requesting metadata.', async(): Promise => { + cache.data = { resource: 'data', 'resource.meta': ' .' }; + metadata = await accessor.getMetadata({ path: `${base}resource` }); + expect(metadata.quads().some((quad): boolean => quad.subject.value === 'this')); + + cache.data = { container: { '.meta': ' .' }}; + metadata = await accessor.getMetadata({ path: `${base}container/` }); + expect(metadata.quads().some((quad): boolean => quad.subject.value === 'this')); + }); + + it('throws an error if there is a problem with the internal metadata.', async(): Promise => { + cache.data = { resource: 'data', 'resource.meta': 'invalid metadata!.' }; + await expect(accessor.getMetadata({ path: `${base}resource` })).rejects.toThrow(); + }); + }); + + describe('writing a data resource', (): void => { + it('throws a 404 if the identifier does not start with the base.', async(): Promise => { + await expect(accessor.writeDocument({ path: 'badpath' }, streamifyArray([]), metadata)) + .rejects.toThrow(NotFoundHttpError); + }); + + it('throws an error when writing to a metadata path.', async(): Promise => { + await expect(accessor.writeDocument({ path: `${base}resource.meta` }, streamifyArray([]), metadata)) + .rejects.toThrow(new ConflictHttpError('Not allowed to create files with the metadata extension.')); + }); + + it('writes the data to the corresponding file.', async(): Promise => { + const data = streamifyArray([ 'data' ]); + await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined(); + expect(cache.data.resource).toBe('data'); + }); + + it('writes metadata to the corresponding metadata file.', async(): Promise => { + const data = streamifyArray([ 'data' ]); + metadata = new RepresentationMetadata(`${base}res.ttl`, { [CONTENT_TYPE]: 'text/turtle', likes: 'apples' }); + await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).resolves.toBeUndefined(); + expect(cache.data['res.ttl']).toBe('data'); + expect(cache.data['res.ttl.meta']).toMatch(`<${base}res.ttl> "apples".`); + }); + + it('does not write metadata that is stored by the file system.', async(): Promise => { + const data = streamifyArray([ 'data' ]); + metadata.add(RDF.type, toNamedNode(LDP.Resource)); + await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined(); + expect(cache.data.resource).toBe('data'); + expect(cache.data['resource.meta']).toBeUndefined(); + }); + + it('throws if something went wrong writing a file.', async(): Promise => { + const data = streamifyArray([ 'data' ]); + data.read = (): any => { + data.emit('error', new Error('error')); + return null; + }; + await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)) + .rejects.toThrow(new Error('error')); + }); + + it('deletes the metadata file if something went wrong writing the file.', async(): Promise => { + const data = streamifyArray([ 'data' ]); + data.read = (): any => { + data.emit('error', new Error('error')); + return null; + }; + metadata.add('likes', 'apples'); + await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)) + .rejects.toThrow(new Error('error')); + expect(cache.data['resource.meta']).toBeUndefined(); + }); + }); + + describe('writing a container', (): void => { + it('throws a 404 if the identifier does not start with the base.', async(): Promise => { + await expect(accessor.writeContainer({ path: 'badpath' }, metadata)).rejects.toThrow(NotFoundHttpError); + }); + + it('creates the corresponding directory.', async(): Promise => { + await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); + expect(cache.data.container).toEqual({}); + }); + + it('can handle the directory already existing.', async(): Promise => { + cache.data.container = {}; + await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); + expect(cache.data.container).toEqual({}); + }); + + it('throws other errors when making a directory.', async(): Promise => { + jest.requireMock('fs').promises.mkdir = (): any => { + throw new Error('error'); + }; + await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow(new Error('error')); + }); + + it('writes metadata to the corresponding metadata file.', async(): Promise => { + metadata = new RepresentationMetadata(`${base}container/`, { likes: 'apples' }); + await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); + expect(cache.data.container).toEqual({ '.meta': expect.stringMatching(`<${base}container/> "apples".`) }); + }); + + it('overwrites existing metadata.', async(): Promise => { + cache.data.container = { '.meta': `<${base}container/> "pears".` }; + metadata = new RepresentationMetadata(`${base}container/`, { likes: 'apples' }); + await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); + expect(cache.data.container).toEqual({ '.meta': expect.stringMatching(`<${base}container/> "apples".`) }); + }); + + it('does not write metadata that is stored by the file system.', async(): Promise => { + metadata = new RepresentationMetadata( + `${base}container/`, + { [RDF.type]: [ toNamedNode(LDP.BasicContainer), toNamedNode(LDP.Resource) ]}, + ); + await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined(); + expect(cache.data.container).toEqual({}); + }); + }); + + describe('deleting a resource', (): void => { + it('throws a 404 if the identifier does not start with the base.', async(): Promise => { + await expect(accessor.deleteResource({ path: 'badpath' })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if the identifier does not match an existing entry.', async(): Promise => { + await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if it matches something that is no file or directory.', async(): Promise => { + cache.data = { resource: 5 }; + await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(NotFoundHttpError); + }); + + it('throws a 404 if the trailing slash does not match its type.', async(): Promise => { + cache.data = { resource: 'apple', container: {}}; + await expect(accessor.deleteResource({ path: `${base}resource/` })).rejects.toThrow(NotFoundHttpError); + await expect(accessor.deleteResource({ path: `${base}container` })).rejects.toThrow(NotFoundHttpError); + }); + + it('deletes the corresponding file for data resources.', async(): Promise => { + cache.data = { resource: 'apple' }; + await expect(accessor.deleteResource({ path: `${base}resource` })).resolves.toBeUndefined(); + expect(cache.data.resource).toBeUndefined(); + }); + + it('throws error if there is a problem with deleting existing metadata.', async(): Promise => { + cache.data = { resource: 'apple', 'resource.meta': {}}; + await expect(accessor.deleteResource({ path: `${base}resource` })).rejects.toThrow(); + }); + + it('removes the corresponding folder for containers.', async(): Promise => { + cache.data = { container: {}}; + await expect(accessor.deleteResource({ path: `${base}container/` })).resolves.toBeUndefined(); + expect(cache.data.container).toBeUndefined(); + }); + + it('removes the corresponding metadata.', async(): Promise => { + cache.data = { container: { resource: 'apple', 'resource.meta': 'metaApple', '.meta': 'metadata' }}; + await expect(accessor.deleteResource({ path: `${base}container/resource` })).resolves.toBeUndefined(); + expect(cache.data.container.resource).toBeUndefined(); + expect(cache.data.container['resource.meta']).toBeUndefined(); + await expect(accessor.deleteResource({ path: `${base}container/` })).resolves.toBeUndefined(); + expect(cache.data.container).toBeUndefined(); + }); + }); +});