var assert = require('chai').assert; var $rdf = require('rdflib'); var ns = require('../lib/vocab/ns.js').ns; var LDP = require('../lib/ldp'); // Helper functions for the FS var rm = require('./test-utils').rm; var write = require('./test-utils').write; var cp = require('./test-utils').cp; var read = require('./test-utils').read; var fs = require('fs'); describe('LDP', function () { var ldp = new LDP(); describe('readFile', function () { it ('return 404 if file does not exist', function (done) { ldp.readFile('resources/unexistent.ttl', function(err) { assert.equal(err.status, 404); done(); }); }); it ('return file if file exists', function (done) { // file can be empty as well write('hello world', 'fileExists.txt'); ldp.readFile(__dirname + '/resources/fileExists.txt', function(err, file) { rm('fileExists.txt'); assert.notOk(err); assert.equal(file, 'hello world'); done(); }); }); }); describe('readContainerMeta', function () { it ('should return 404 if .meta is not found', function (done) { ldp.readContainerMeta('resources/', function(err) { assert.equal(err.status, 404); done(); }); }); it ('should return content if metaFile exists', function (done) { // file can be empty as well write('This function just reads this, does not parse it', '.meta'); ldp.readContainerMeta(__dirname + '/resources/', function(err, metaFile) { rm('.meta'); assert.notOk(err); assert.equal(metaFile, 'This function just reads this, does not parse it'); done(); }); }); it ('should work also if trailing `/` is not passed', function (done) { // file can be empty as well write('This function just reads this, does not parse it', '.meta'); ldp.readContainerMeta(__dirname + '/resources', function(err, metaFile) { rm('.meta'); assert.notOk(err); assert.equal(metaFile, 'This function just reads this, does not parse it'); done(); }); }); }); describe('put', function() { it('should write a file in an existing dir', function(done) { ldp.put(__dirname + '/resources/testPut.txt', 'hello world', function (err) { assert.notOk(err); var found = read('testPut.txt'); rm('testPut.txt'); assert.equal(found, 'hello world'); done(); }); }); it('should fail if a trailing `/` is passed', function(done) { ldp.put(__dirname + '/resources/', 'hello world', function (err) { assert.equal(err.status, 409); done(); }); }); }); describe('listContainer', function () { it('should inherit type if file is .ttl', function(done) { write('@prefix dcterms: .' + '@prefix o: .' + '<> a ;' + ' dcterms:title "This is a magic type" ;' + ' o:limit 500000.00 .', 'sampleContainer/magicType.ttl'); ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) { var graph = $rdf.graph(); $rdf.parse( data, graph, 'https://server.tld/sampleContainer', 'text/turtle'); var statements = graph .each( $rdf.sym('https://server.tld/magicType.ttl'), ns.rdf('type'), undefined) .map(function(d) { return d.uri; }); assert.equal(statements.length, 2); assert.isAbove(statements.indexOf('http://www.w3.org/ns/ldp#MagicType'), -1); assert.isAbove(statements.indexOf('http://www.w3.org/ns/ldp#Resource'), -1); rm('sampleContainer/magicType.ttl'); done(); }); }); it('should not inherit type of BasicContainer/Container if type is File', function(done) { write('@prefix dcterms: .' + '@prefix o: .' + '<> a ;' + ' dcterms:title "This is a container" ;' + ' o:limit 500000.00 .', 'sampleContainer/containerFile.ttl'); write('@prefix dcterms: .' + '@prefix o: .' + '<> a ;' + ' dcterms:title "This is a container" ;' + ' o:limit 500000.00 .', 'sampleContainer/basicContainerFile.ttl'); ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) { var graph = $rdf.graph(); $rdf.parse( data, graph, 'https://server.tld/sampleContainer', 'text/turtle'); var basicContainerStatements = graph.each( $rdf.sym('https://server.tld/basicContainerFile.ttl'), ns.rdf('type'), undefined); assert.equal(basicContainerStatements.length, 1); var containerStatements = graph.each( $rdf.sym('https://server.tld/containerFile.ttl'), ns.rdf('type'), undefined); assert.equal(containerStatements.length, 1); basicContainerStatements.forEach(function(statement) { assert.equal(statement.uri, ns.ldp('Resource').uri); }); containerStatements.forEach(function(statement) { assert.equal(statement.uri, ns.ldp('Resource').uri); }); rm('sampleContainer/containerFile.ttl'); rm('sampleContainer/basicContainerFile.ttl'); done(); }); }); it('should ldp:contains the same amount of files in dir', function(done) { ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) { fs.readdir(__dirname + '/resources/sampleContainer/', function(err, files) { var graph = $rdf.graph(); $rdf.parse( data, graph, 'https://server.tld/sampleContainer', 'text/turtle'); var statements = graph.each( undefined, ns.ldp('contains'), undefined); assert.notEqual(graph.statements.length, 0); assert.equal(statements.length, files.length); assert.notOk(err); done(); }); }); }); }); });