+
/**
* Eosコマンドをエミュレートするクラス
* @varructor
var eos = {
validate: validate,
toExecString: toExecString,
- execute: execute
+ execute: execute,
+ getFiles: getFiles
}
// include all Eos command's info.
// For seaching with O(n), the object key name is command name.
-var commandReference = {
- mrcImageInfo: { },
- dcdFilePrint: { }
-};
+var db = require('./DB.js').instance;
+var commandList = require(__dirname + '/../../user-specific-files/OptionControlFile/command_list.json');
+var ocfReference = {};
+
+commandList.forEach(function(c) {
+ ocfReference[c.name] = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + c.name);
+});
+
// for unit test
-var workspace = ['file1.txt', 'file2.txt'];
/**
* Class variables
*/
-
-/**
- * validate
- * コマンドとオプションのバリデーション
- * @param command
- * @param params
- * @returns {valid: boolean, message: string}
- */
-function validate(command, options) {
- var result = { hasDone: false, // true | false
- comment: ''// string
- };
-
- var ocf; // Array
- var ocfObj = {}; // key-value
-
- try {
- /**
- * Check of command name
- */
- if(typeof command !== 'string') {
- errorMsg = 'Command parameter need to be string';
- throw new Error(errorMsg);
- }
-
- var hasCommand = Object.keys(commandReference).indexOf(command) > -1;
- if(!hasCommand) {
- errorMsg = 'Command name is invalid';
- throw new Error(errorMsg);
- }
-
- /**
- * Check of options
- */
-
- if(!(Array.isArray(options))) {
- errorMsg = 'Options need to be Array';
- throw new Error(errorMsg);
- }
-
-
+function hasOneProperty(options) {
+ return new Promise(function(resolve, reject) {
if(options.length === 0) {
- errorMsg = 'At least one option is required.';
- throw new Error(errorMsg);
- }
-
- // translate options to key-value and check whether options include correct member
- var optionsObj = {};
- var hasCorrectMember = true;
- var isArgumentsArray = true;
-
- options.forEach(function(o) {
- if(!(o.name) && !(o.arguments)) {
- hasCorrectMember = false;
- } else {
- if(Array.isArray(o.arguments)) {
- optionsObj[o.name] = o.arguments;
- } else {
- isArgumentsArray = false;
- }
- }
- });
-
- // check each object has proberties "name" and "argumets"
- if(!hasCorrectMember) {
- errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
+ var errorMsg = 'At least one option is required.';
throw new Error(errorMsg);
}
+ });
+}
- // check each "argumets" properties is Array
- if(!isArgumentsArray) {
- errorMsg = 'Each "arguments" properties needs to be Array';
- throw new Error(errorMsg);
- }
-
- // Read OptionControlFile info of command
- ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
-
- // translate ocf info to key-value
+function matchOption(options, ocf) {
+ return new Promise(function(resolve, reject) {
+ var ok = {};
var notIncludingRequiredOptions = [];
+ options.forEach(function(o) {
+ ok[o.name] = o.arguments;
+ });
ocf.forEach(function(o) {
- if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
+ if(o.optionProperties && !ok[o.option]) {
notIncludingRequiredOptions.push(o.option);
}
- ocfObj[o.option] = o;
});
// check whether all required option exist
if(notIncludingRequiredOptions.length > 0) {
- errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
- throw new Error(errorMsg);
+ var errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
+ reject(new Error(errorMsg));
+ } else {
+ resolve();
}
+ });
+}
- var invalidArgumentsNumber= [];
- var invalidArgumentType = [];
- var invalidOutputFileName = [];
-
- // output file Regexp
- var outRegExp = /out|append/;
-
+function validArgumentsNumber(options, ocfObj) {
+ return new Promise(function(resolve, reject) {
options.forEach(function(o) {
// option number
var expectNum = ocfObj[o.name].optionNumber;
var actualNum = o.arguments.length;
if(expectNum !== actualNum) {
- invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
+ reject(new Error('Invalid arguments number'));
}
+ });
+ resolve();
+ });
+};
-
- // argType and outFile name
+function validArgumentsType(options, ocfObj, workspace) {
+ return new Promise(function(resolve, reject) {
+ options.forEach(function(o) {
o.arguments.forEach(function(arg,i) {
// argType
var formType = ocfObj[o.name].arg[i].formType
if(formType === 'select') { // This argument is filename
var exist = workspace.indexOf(arg) > -1;
if(!exist) {
- invalidArgumentType.push({name: o.name, file: arg});
+ reject(new Error(arg + ' doesn\'t exist.'));
}
} else {
var expectType = formType === 'text' ? 'string' : 'number';
var actualType = typeof arg;
if(expectType !== actualType) {
- invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
+ reject(new Error('argType is invalid'));
}
}
+ });
+ });
+ resolve();
+ });
+}
+function validOutfileName(options, ocfObj, workspace) {
+ return new Promise(function(resolve, reject) {
+ // output file Regexp
+ var outRegExp = /out|append/;
+
+ options.forEach(function(o) {
+ o.arguments.forEach(function(arg,i) {
// outFile name
if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
if(workspace.indexOf(o.arguments[i]) > -1) {
- invalidOutputFileName.push({name: o.name, file: arg});
+ reject(new Error('Invalid outfile name.'));
}
}
});
});
+ resolve();
+ });
+}
- // check arguments number value
- if(invalidArgumentsNumber.length > 0) {
- errorMsg = '"arguments" properties is invalid number.\n';
- invalidArgumentsNumber.forEach(function(i) {
- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
- });
- throw new Error(errorMsg);
+/**
+ * validate
+ * コマンドとオプションのバリデーション
+ * @param command
+ * @param params
+ * @returns {valid: boolean, message: string}
+ */
+function validate(command, options, workspaceId) {
+ return new Promise(function(resolve, reject) {
+
+ var ocf;
+ if(ocfReference[command]) {
+ ocf = ocfReference[command];
+ } else {
+ var errorMsg = 'Command name is invalid';
+ reject(new Error(errorMsg));
}
- // check arguments type
- if(invalidArgumentType.length > 0) {
- errorMsg = '"arguments" type is invalid.\n';
- invalidArgumentType.forEach(function(i) {
- if(i.file) {
- errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
+ var ocfObj = {};
+ ocf.forEach(function(o) {
+ ocfObj[o.option] = o;
+ });
+
+ var optionsObj = {};
+ if(Array.isArray(options)) {
+ options.forEach(function(o) {
+ if(o.name && o.arguments) {
+ if(Array.isArray(o.arguments)) {
+ optionsObj[o.name] = o.arguments;
+ } else {
+ var errorMsg = 'Each "arguments" properties needs to be Array';
+ reject(new Error(errorMsg));
+ }
} else {
- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
+ var errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
+ reject(new Error(errorMsg));
}
});
- throw new Error(errorMsg);
+ } else {
+ var errorMsg = 'Options need to be Array';
+ reject(new Error(errorMsg));
}
- // check outFile name
- if(invalidOutputFileName.length > 0) {
- errorMsg = 'output file name is invalid.\n';
- invalidOutputFileName.forEach(function(i) {
- errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
+ getFiles(workspaceId)
+ .then(function(workspace) {
+
+ // validate function
+ var promises = [];
+ promises.push(hasOneProperty(options));
+ promises.push(matchOption(options, ocf));
+ promises.push(validArgumentsNumber(options, ocfObj));
+ promises.push(validArgumentsType(options, ocfObj, workspace));
+ promises.push(validOutfileName(options, ocfObj, workspace));
+
+ // do validation
+ return Promise.all(promises)
+ .then(function() {
+ resolve('Success');
+ })
+ .catch(function(error) {
+ reject(error);
});
- throw new Error(errorMsg);
+ });
+ });
+ /*
+ var invalidArgumentType = [];
+ var invalidOutputFileName = [];
+
+ // output file Regexp
+ var outRegExp = /out|append/;
+
+ options.forEach(function(o) {
+ // outFile name
+ if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
+ if(workspace.indexOf(o.arguments[i]) > -1) {
+ invalidOutputFileName.push({name: o.name, file: arg});
+ }
+ }
+ });
+ });
+
+ // check arguments number value
+ if(invalidArgumentsNumber.length > 0) {
+ errorMsg = '"arguments" properties is invalid number.\n';
+ invalidArgumentsNumber.forEach(function(i) {
+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
+ });
+ throw new Error(errorMsg);
+ }
+
+ // check arguments type
+ if(invalidArgumentType.length > 0) {
+ errorMsg = '"arguments" type is invalid.\n';
+ invalidArgumentType.forEach(function(i) {
+ if(i.file) {
+ errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
+ } else {
+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
+ }
+ });
+ throw new Error(errorMsg);
+ }
+
+ // check outFile name
+ if(invalidOutputFileName.length > 0) {
+ errorMsg = 'output file name is invalid.\n';
+ invalidOutputFileName.forEach(function(i) {
+ errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
+ });
+ throw new Error(errorMsg);
+ }
+ } catch(e) {
+ result.message = e.message;
+ return result;
}
- } catch(e) {
- result.message = e.message;
- return result;
- }
+ });
+ */
}
/**
// set default parameters
ocf.forEach(function(o) {
- o.arg.forEach(function(arg) {
- if(!(arg.initialValue === "") && arg.initialValue) {
- if(!(finalOptions[o.option])) {
+ o.arg.forEach(function(arg) {
+ if(!(arg.initialValue === "") && arg.initialValue) {
+ if(!(finalOptions[o.option])) {
finalOptions[o.option] = [];
finalOptions[o.option].push(arg.initialValue);
- } else {
+ } else {
finalOptions[o.option].push(arg.initialValue);
- }
- }
- });
- });
+ }
+ }
+ });
+ });
// set user setting parameters
options.forEach(function(o) {
- finalOptions[o.name] = o.arguments;
- });
+ finalOptions[o.name] = o.arguments;
+ });
// set execution string
Object.keys(finalOptions).forEach(function(key) {
- execStr += key + ' ';
- finalOptions[key].forEach(function(arg) {
- execStr += arg + ' ';
- });
- });
+ execStr += key + ' ';
+ finalOptions[key].forEach(function(arg) {
+ execStr += arg + ' ';
+ });
+ });
// remove last blank
execStr = execStr.slice(0,execStr.length-1);
}
}
+/**
+ * getFiles
+ * @param fileId
+ * @returns {promise} resolve(Array)
+ */
+function getFiles(fileId) {
+ return new Promise(function(resolve, reject) {
+ if(process.env.NODE_ENV) {
+ resolve(['file1.txt', 'file2.txt']);
+ } else {
+ db.getFiles(fileId)
+ .then(function(r) {
+ var workspace = r.map(function(f) { return f.name });
+ resolve(workspace);
+ });
+ }
+ });
+}
+
module.exports = { instance: eos };
(function() {
'use strict';
var eos = require('../../server/class/Eos').instance;
- var expect = require('chai').expect;
+ var chai = require('chai');
+ var chaiAsPromised = require('chai-as-promised');
+ chai.use(chaiAsPromised);
+ var expect = chai.expect;
+ chai.should();
+
+ var db = require('../../server/class/DB').instance;
var test1 = `
/**
describe(test1, function() {
- it('should return false when the command is typeof number.', function() {
- var result = eos.validate(2);
- expect(result.message).to.equal('Command parameter need to be string');
+ it('should return Array', function() {
+ return expect(eos.getFiles()).to.eventually.be.length(2);
});
- it('should return false when command name is invalid', function() {
- var result = eos.validate('hoge');
- expect(result.message).to.equal('Command name is invalid');
+ it('should be rejected(2)', function() {
+ return eos.validate('hoge').should.be.rejectedWith(Error, 'Command name is invalid');
});
- it('should return false when options is not Array', function() {
- var result = eos.validate('mrcImageInfo', {});
- expect(result.message).to.equal('Options need to be Array');
+ it('should be rejected(3)', function() {
+ return eos.validate('mrcImageNoiseAdd').should.be.rejectedWith(Error, 'Options need to be Array');
});
- it('should return false when options is Array whose length is 0.', function() {
- var result = eos.validate('mrcImageInfo', []);
- expect(result.message).to.equal('At least one option is required.');
+ it('should be rejected(4)', function() {
+ return eos.validate('mrcImageNoiseAdd', []).should.be.rejectedWith(Error, 'At least one option is required.');
});
- it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
- var result = eos.validate('mrcImageInfo', [{hoge: 'hoge'}]);
- expect(result.message).to.equal('Options need to include Object which have properties "name" and "arguments"');
+ it('should return false when options is not Array', function() {
+ return eos.validate('mrcImageNoiseAdd', {}).should.be.rejectedWith(Error, 'Options need to be Array');
});
- it('should return false when "argumetns" properties are not Array', function() {
- var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
- expect(result.message).to.equal('Each "arguments" properties needs to be Array');
+ it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
+ return eos.validate('mrcImageInfo', [{hoge: 'hoge'}]).should.be.rejectedWith(Error, 'Options need to include Object which have properties "name" and "arguments"');
});
-
it('should return false when "argumetns" properties are not Array', function() {
- var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]);
- expect(result.message).to.equal('Each "arguments" properties needs to be Array');
+ return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]).should.be.rejectedWith(Error,'Each "arguments" properties needs to be Array');
});
it('should return false when required options do not exist', function() {
- var result = eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]);
- expect(result.message).to.equal('Option -i are required');
+ return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]).should.be.rejectedWith('Option -i are required');
});
it('should return false when required options do not exist', function() {
- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]);
- expect(result.hasDone).to.equal(false);
- console.log(result.message);
+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]).should.be.rejectedWith(Error, 'Invalid arguments number');
});
it('should return false when input file does not exist', function() {
- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]);
- expect(result.hasDone).to.equal(false);
- console.log(result.message);
-
+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]).should.be.rejectedWith(Error, 'hoge.txt doesn\'t exist.');
});
it('should return false when output file is not string', function() {
- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]);
- expect(result.hasDone).to.equal(false);
- console.log(result.message);
-
+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]).should.be.rejectedWith(Error, 'argType is invalid');
});
it('should return false when output file has already existed', function() {
- var result = eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]);
- expect(result.hasDone).to.equal(false);
- console.log(result.message);
-
+ return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]).should.be.rejectedWith(Error, 'Invalid outfile name.');
});
- });
+ /*
describe(test2, function() {
it('should return true when all options is proper.', function() {
var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i file1.txt -o file3.txt');
});
+ */
+ });
+
+ describe('getFiles', function() {
+ before(function() {
+ process.env.NODE_ENV = '';
+ return db.init()
+ .then(function() {
+ return Promise.all([db.test1(), db.test2(), db.testRest()])
+ });
+ });
+
+ it('should be resolved with length 4', function() {
+ return expect(eos.getFiles('1f83f620-c1ed-11e5-9657-7942989daa00')).to.eventually.length(4);
+ });
+
+ after(function() {
+ process.env.NODE_ENV = 'test';
+ });
});
});
})();