|
| 1 | +import arrayifyStream from 'arrayify-stream'; |
| 2 | +import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError'; |
| 3 | +import { QuadRepresentation } from '../../../src/ldp/representation/QuadRepresentation'; |
| 4 | +import { Readable } from 'stream'; |
| 5 | +import { SimpleResourceStore } from '../../../src/storage/SimpleResourceStore'; |
| 6 | +import streamifyArray from 'streamify-array'; |
| 7 | +import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/UnsupportedMediaTypeHttpError'; |
| 8 | +import { namedNode, triple } from '@rdfjs/data-model'; |
| 9 | + |
| 10 | +const base = 'http://test.com/'; |
| 11 | + |
| 12 | +describe('A SimpleResourceStore', (): void => { |
| 13 | + let store: SimpleResourceStore; |
| 14 | + let representation: QuadRepresentation; |
| 15 | + const quad = triple( |
| 16 | + namedNode('http://test.com/s'), |
| 17 | + namedNode('http://test.com/p'), |
| 18 | + namedNode('http://test.com/o'), |
| 19 | + ); |
| 20 | + |
| 21 | + beforeEach(async(): Promise<void> => { |
| 22 | + store = new SimpleResourceStore(base); |
| 23 | + |
| 24 | + representation = { |
| 25 | + data: streamifyArray([ quad ]), |
| 26 | + dataType: 'quad', |
| 27 | + metadata: null, |
| 28 | + }; |
| 29 | + }); |
| 30 | + |
| 31 | + it('errors if a resource was not found.', async(): Promise<void> => { |
| 32 | + await expect(store.getRepresentation({ path: `${base}wrong` }, {})).rejects.toThrow(NotFoundHttpError); |
| 33 | + await expect(store.addResource({ path: 'http://wrong.com/wrong' }, null)).rejects.toThrow(NotFoundHttpError); |
| 34 | + await expect(store.deleteResource({ path: 'wrong' })).rejects.toThrow(NotFoundHttpError); |
| 35 | + await expect(store.setRepresentation({ path: 'http://wrong.com/' }, null)).rejects.toThrow(NotFoundHttpError); |
| 36 | + }); |
| 37 | + |
| 38 | + it('errors when modifying resources.', async(): Promise<void> => { |
| 39 | + await expect(store.modifyResource()).rejects.toThrow(Error); |
| 40 | + }); |
| 41 | + |
| 42 | + it('errors for wrong input data types.', async(): Promise<void> => { |
| 43 | + (representation as any).dataType = 'binary'; |
| 44 | + await expect(store.addResource({ path: base }, representation)).rejects.toThrow(UnsupportedMediaTypeHttpError); |
| 45 | + }); |
| 46 | + |
| 47 | + it('can write and read data.', async(): Promise<void> => { |
| 48 | + const identifier = await store.addResource({ path: base }, representation); |
| 49 | + expect(identifier.path.startsWith(base)).toBeTruthy(); |
| 50 | + const result = await store.getRepresentation(identifier, {}); |
| 51 | + expect(result).toEqual({ |
| 52 | + dataType: 'quad', |
| 53 | + data: expect.any(Readable), |
| 54 | + metadata: { |
| 55 | + profiles: [], |
| 56 | + raw: [], |
| 57 | + }, |
| 58 | + }); |
| 59 | + await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ quad ]); |
| 60 | + }); |
| 61 | + |
| 62 | + it('can read binary data.', async(): Promise<void> => { |
| 63 | + const identifier = await store.addResource({ path: base }, representation); |
| 64 | + expect(identifier.path.startsWith(base)).toBeTruthy(); |
| 65 | + const result = await store.getRepresentation(identifier, { type: [{ value: 'text/turtle', weight: 1 }]}); |
| 66 | + expect(result).toEqual({ |
| 67 | + dataType: 'binary', |
| 68 | + data: expect.any(Readable), |
| 69 | + metadata: { |
| 70 | + profiles: [], |
| 71 | + raw: [], |
| 72 | + contentType: 'text/turtle', |
| 73 | + }, |
| 74 | + }); |
| 75 | + await expect(arrayifyStream(result.data)).resolves.toContain( |
| 76 | + `<${quad.subject.value}> <${quad.predicate.value}> <${quad.object.value}>`, |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('can set data.', async(): Promise<void> => { |
| 81 | + await store.setRepresentation({ path: base }, representation); |
| 82 | + const result = await store.getRepresentation({ path: base }, {}); |
| 83 | + expect(result).toEqual({ |
| 84 | + dataType: 'quad', |
| 85 | + data: expect.any(Readable), |
| 86 | + metadata: { |
| 87 | + profiles: [], |
| 88 | + raw: [], |
| 89 | + }, |
| 90 | + }); |
| 91 | + await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ quad ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('can delete data.', async(): Promise<void> => { |
| 95 | + await store.deleteResource({ path: base }); |
| 96 | + await expect(store.getRepresentation({ path: base }, {})).rejects.toThrow(NotFoundHttpError); |
| 97 | + }); |
| 98 | +}); |
0 commit comments