Skip to content

Commit

Permalink
#251 First implementation and tests with localstorage.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeijer committed Mar 17, 2015
1 parent 4119a48 commit a8e6ab7
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
129 changes: 129 additions & 0 deletions src/common/util/opencontext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*jshint node:true*/
/**
* @author pmeijer / https://github.com/pmeijer
*/

define(['util/assert', 'common/core/core'], function (ASSERT, Core) {

/**
* Opens the context specified by the parameters and returns a result object containing
* data.
* @param {object} storage - clientstorage, serveruserstorage or localstorage
* @param {object} gmeConfig - global webgme configuration
* @param {object} parameters
* @param {string} [parameters.projectName] - name of project to open -> result.project
* @param {string} [parameters.branchName] - name of branch to load root from. -> result.rootNode, result.commitHash
* @param {string} [parameters.commitHash] - if branchName not given commitHash will be loaded. -> result.rootNode
* @param {string} [parameters.nodeIds] - //TODO: will load all specified node ids. -> result.nodes
* @param {boolean} [parameters.meta] - //TODO: will load all META-nodes. -> result.META
* @param {object} [parameters.core] - Used if branchName or commitHash is specified (a new Core will be created
* if needed and not provided here). -> result.core
* @param {function} callback
*/
var openContext = function (storage, gmeConfig, parameters, callback) {
var result = {},
core,
closeOnError = function (err) {
if (result.project) {
result.project.closeProject(function () {
storage.closeDatabase(function () {
callback(err);
});
});
} else {
storage.closeDatabase(function () {
callback(err);
});
}
};

ASSERT(typeof storage !== 'undefined' && storage.hasOwnProperty('openDatabase'), 'storage must be given');
ASSERT(typeof callback === 'function', 'a callback must be given');

storage.openDatabase(function (err) {
if (err) {
closeOnError(err);
return;
}
if (parameters.projectName) {
storage.getProjectNames(function (err, projectNames) {
if (err) {
closeOnError(err);
return;
}
if (projectNames.indexOf(parameters.projectName) === -1) {
closeOnError('"' + parameters.projectName + '" does not exists among: ' +
projectNames.toString());
return;
}
storage.openProject(parameters.projectName, function (err, project) {
if (err) {
closeOnError(err);
return;
}
result.project = project;
if (parameters.branchName || parameters.commitHash) {
_getCommitHash(result.project, gmeConfig, parameters, function (err, commitHash) {
if (err) {
closeOnError(err);
return;
}
result.commitHash = commitHash;
_loadRoot(project, gmeConfig, commitHash, parameters, function (err, rootNode, core) {
if (err) {
closeOnError(err);
return;
}
result.rootNode = rootNode;
result.core = core;
callback(null, result);
});
});
} else {
callback(null, result);
}
});
});
} else {
callback(null, result);
}
});
};

function _getCommitHash(project, gmeConfig, parameters, callback) {
if (parameters.branchName) {
project.getBranchNames(function (err, names) {
if (err) {
callback(err);
} else if (names.hasOwnProperty(parameters.branchName) === false) {
callback('"' + parameters.branchName + '" not in project: "' +
parameters.projectName + '".');
} else {
callback(null, names[parameters.branchName]);
}
});
} else {
callback(null, parameters.commitHash);
}
}

function _loadRoot(project, gmeConfig, commitHash, parameters, callback) {
var core;
project.loadObject(commitHash, function (err, commitObj) {
if (err) {
callback(err);
return;
}
core = parameters.core || new Core(project, {globConf: gmeConfig});
core.loadRoot(commitObj.root, function (err, rootNode) {
if (err) {
callback(err);
return;
}
callback(null, rootNode, core);
});
});
}

return openContext;
});
105 changes: 105 additions & 0 deletions test/common/util/opencontext.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*jshint node:true, mocha:true*/
/**
* @author pmeijer / https://github.com/pmeijer
*/

var testFixture = require('../../_globals');

describe('openContext', function () {
'use strict';

var expect = testFixture.expect,
WebGME = testFixture.WebGME,
openContext = testFixture.requirejs('common/util/opencontext'),
Core = testFixture.WebGME.core;

describe('using local-storage', function () {
var storage,// Will get local one from importProject.
project,
commitHash,
gmeConfig = testFixture.getGmeConfig();

before(function (done) {
var importParam = {
filePath: './test/asset/sm_basic.json',
projectName: 'doesExist',
branchName: 'master',
gmeConfig: gmeConfig
};

importParam.storage = storage;
testFixture.importProject(importParam, function (err, result) {
storage = result.storage;
commitHash = result.commitHash;
done(err);
});
});

afterEach(function (done) {
if (project) {
project.closeProject(function (err) {
done(err);
});
} else {
done();
}
});

after(function (done) {
storage.closeDatabase(function (err) {
done(err);
});
})

it('should open existing project', function (done) {
var parameters = {
projectName: 'doesExist'
};
openContext(storage, gmeConfig, parameters, function (err, result) {
expect(err).equal(null);
expect(result).to.have.keys('project');
done();
});
});

it('should return error with non-existing project', function (done) {
var parameters = {
projectName: 'doesNotExist'
};
openContext(storage, gmeConfig, parameters, function (err, result) {
expect(err).to.equal('"doesNotExist" does not exists among: doesExist');
done();
});
});

it('should load existing branch', function (done) {
var parameters = {
projectName: 'doesExist',
branchName: 'master'
};
openContext(storage, gmeConfig, parameters, function (err, result) {
expect(err).equal(null);
expect(result).to.have.keys('project', 'rootNode', 'commitHash', 'core');
done();
});
});

it('should return error with non-existing branchName', function (done) {
var parameters = {
projectName: 'doesExist',
branchName: 'b1_lancer'
};
openContext(storage, gmeConfig, parameters, function (err, result) {
console.log(err);
expect(err).to.equal('"b1_lancer" not in project: "doesExist".');
done();
});
});
});

//storage = new WebGME.clientStorage({
// globConf: gmeConfig,
// type: 'node',
// host: (gmeConfig.server.https.enable === true ? 'https' : 'http') + '://127.0.0.1'
//});
});

0 comments on commit a8e6ab7

Please sign in to comment.