Commit fbcf9f9f authored by Maxime Chaillet's avatar Maxime Chaillet

reshape doiController. Write tests on getBeamlineUrl(). It fixes #55

parent 14e1ad71
// Karma configuration
// Generated on Thu Oct 11 2018 23:09:22 GMT+0200 (CEST)
module.exports = function(config) {
config.set({
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'browserify'],
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'browserify'],
// list of files / patterns to load in the browser
files: [
'src/jsonextractor.js',
'src/constants.js',
'tests/jsonExtractor.test.js',
'bower_components/lodash/lodash.js'
],
// list of files / patterns to load in the browser
files: [
'src/beamlineurl.js',
'src/constants.js',
'src/doicontroller.js',
'src/doiview.js',
'src/experimentalreportcontroller.js',
'src/event.js',
'src/jsonextractor.js',
'tests/jsonExtractor.test.js',
'tests/doiController.test.js',
'bower_components/lodash/lodash.js',
'bower_components/moment/moment.js'
],
// list of files / patterns to exclude
exclude: [
],
// list of files / patterns to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'tests/jsonExtractor.test.js': [ 'browserify' ]
},
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'tests/jsonExtractor.test.js': ['browserify']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Firefox', 'Chrome' ],
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Firefox', 'Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
This diff is collapsed.
This diff is collapsed.
var CONSTANTS = {
ES_RESOURCE_TYPE : "experimental session resource type",
DC_RESOURCE_TYPE : "dataset collection resource type"
};
//module.exports = CONSTANTS;
};
\ No newline at end of file
function DoiData() {
function DoiData(title, doi, dataciteLink, publisher, creator, publication_year) {
this.title = title;
this.doi = doi;
this.dataciteLink = dataciteLink;
this.publisher = publisher;
this.creator = creator;
this.publiclyAccessibleYear = publication_year;
}
/**
* Get the accessibility status
* @param {string} resourceType the resource type for this DOI
......
function DOIController(view) {
var _this = this;
// doiServer = the server which provides doi details from a given doi name.
//this.doiServer = "https://data.datacite.org/";
this.doiServer = "https://api.test.datacite.org/";
this.doiServer = "https://data.datacite.org/";
// 10.5072 is the test esrf doi prefix;
......@@ -50,9 +49,8 @@ DOIController.prototype.getData = function (doi) {
timeout: 15000, // triggers timeout when request pends longer than
// 5000ms
url: this.doiServer + doi,
beforeSend: function (xhr) {
beforeSend: function () {
_this.view.setLoading("Retrieving data from Server");
xhr.setRequestHeader("Authorization", "Basic " + btoa("DATACITE.ESRF" + ":" + "datacyte1")); // to be removed as well as the parameter
},
complete: function () {
_this.view.setLoading(false);
......@@ -65,56 +63,37 @@ DOIController.prototype.getData = function (doi) {
if (!_this.isValidDOIData(data)) {
_this.view.renderError("Not Found", _this.origin);
} else {
var doiData = new DoiData();
doiData.title = data.title;
doiData.doi = data.doi.toUpperCase();
doiData.dataciteLink = _this.doiServer + doi;
doiData.publisher = data.publisher;
doiData.creator = data.creator;
// Extract data and fill doiData with it
var doiData = new DoiData(data.title, data.doi.toUpperCase(), _this.doiServer + doi, data.publisher, data.creator, data.publication_year);
var jsonExtractor = new JsonExtractor();
doiData.abstract = jsonExtractor.getAbstract(data);
doiData.investigationId = jsonExtractor.extractInvestigationId(data);
doiData.mintingYear = jsonExtractor.getMintingYear(data);
doiData.sessionDate = jsonExtractor.getSessionDate(data);
doiData.publiclyAccessibleYear = data.publication_year;
doiData.proposalType = jsonExtractor.getFieldFromSubject(data.subject, "Proposal Type Description");
doiData.proposalNames = jsonExtractor.getFieldFromSubject(data.subject, "Proposal");
console.log(doiData.proposalNames);
_this.experimentalReportController.requestFilenameList(doiData.proposalName);
doiData.resourceType = jsonExtractor.getResourceType(data);
doiData.resourceTypeGeneral = jsonExtractor.getResourceTypeGeneral(data);
doiData.sessionDate = jsonExtractor.getSessionDate(data);
doiData.beamlines = jsonExtractor.getFieldFromSubject(data.subject, "Instrument");
var proposalNames = jsonExtractor.getFieldFromSubject(data.subject, "Proposal");
var beamlines = jsonExtractor.getFieldFromSubject(data.subject, "Instrument");
doiData.investigationId = jsonExtractor.extractInvestigationId(data);
doiData.resourceType = jsonExtractor.getResourceType(data);
_this.experimentalReportController.requestFilenameList(proposalNames[0]);
doiData.accessibility = doiData.getAccessibilityStatus(doiData.resourceType, doiData.sessionDate, doiData.publiclyAccessibleYear);
doiData.accessMessage = _this.view.setDataAccessMessage(doiData);
doiData.citation = _this.createCitation(data);
doiData.resourceTypeGeneral = jsonExtractor.getResourceTypeGeneral(data);
// constants passed for tests in the templates
doiData.DC_RESOURCE_TYPE = CONSTANTS.DC_RESOURCE_TYPE;
doiData.ES_RESOURCE_TYPE = CONSTANTS.ES_RESOURCE_TYPE;
// Set the URL corresponding to the beamline name.
if (doiData.resourceType === CONSTANTS.ES_RESOURCE_TYPE) {
if (data.date_collected) {
var experimentalDate = moment(data.date_collected);
doiData.beamlineUrls = _this.getBeamlineUrl(doiData.beamlines, experimentalDate);
}
}
doiData.beamlineUrls = _this.getBeamlineUrl(beamlines, data.date_collected, doiData.resourceType);
}
if (doiData.resourceType === CONSTANTS.DC_RESOURCE_TYPE) {
if (data.publication_year) {
var publicationDate = moment(data.publication_year, "YYYY");
doiData.beamlineUrls = _this.getBeamlineUrl(doiData.beamlines, publicationDate);
}
}
doiData.beamlineUrls = _this.getBeamlineUrl(beamlines, data.publication_year, doiData.resourceType);
}
doiData.mergedProposalBeamlines = jsonExtractor.getMergedProposalAndBeamline(doiData.proposalNames, doiData.beamlines, doiData.beamlineUrls);
// Create a table containing unique tuples [proposal, beamline, beamlineUrl]
doiData.mergedProposalBeamlines = jsonExtractor.getMergedProposalAndBeamline(proposalNames, beamlines, doiData.beamlineUrls);
// send to dust render
_this.view.renderDOIData(doiData);
}
}
......@@ -169,27 +148,40 @@ DOIController.prototype.setOrigin = function (origin) {
};
/**
* Get the corresponding url for a given beamline name
* @param {array} beamlines The beamline name
* @param {momentJS} date The date the experiment was performed.
* @return {array} an array containing urls of the beamLine(s).
*/
DOIController.prototype.getBeamlineUrl = function (beamlines, date) {
* Get the corresponding url for a given beamline name
* @param {array} beamlines The beamlines
* @param {momentJS} date The date the experiment was performed.
* @param {String} resourceType the data resource type
* @return {array} an array containing urls of the beamLine(s).
*/
DOIController.prototype.getBeamlineUrl = function (beamlines, date, resourceType) {
var result = [];
beamlines.forEach(function (beamline) {
for (var i = 0; i < BEAMLINEURL.length; i++) {
if (date) {
if (resourceType === CONSTANTS.ES_RESOURCE_TYPE) {
date = moment(date);
}
if (resourceType === CONSTANTS.DC_RESOURCE_TYPE) {
date = moment(date, "YYYY");
}
var foundUrl = "";
var startDate = moment(BEAMLINEURL[i].startDate);
var endDate = moment(BEAMLINEURL[i].endDate);
if (BEAMLINEURL[i].name.toLowerCase() === beamline.toLowerCase() && date >= startDate && date <= endDate) {
foundUrl = BEAMLINEURL[i].url;
break;
for (var i = 0; i < BEAMLINEURL.length; i++) {
var startDate = moment(BEAMLINEURL[i].startDate);
var endDate = moment(BEAMLINEURL[i].endDate);
if (BEAMLINEURL[i].name.toLowerCase() === beamline.toLowerCase() && date >= startDate && date <= endDate) {
foundUrl = BEAMLINEURL[i].url;
break;
}
}
}
if (foundUrl === "") {
result.push("no link");
if (foundUrl === "") {
result.push("noLink");
} else {
result.push(foundUrl);
}
} else {
result.push(foundUrl);
result.push("noLink");
}
});
return result;
......
......@@ -154,7 +154,8 @@ DOIView.prototype.renderMetadata = function (data) {
if (data) {
var _this = this;
dust.render('metadata_tpl', {
data: data
data: data,
constants : CONSTANTS
}, function (err, out) {
$('#' + _this.metadataDivId).html(out);
});
......
......@@ -8,17 +8,13 @@ function ExperimentalReportController() {
/**
* Request the experimental report filename list for the given proposal
*
* @param {string}
* proposal The proposal
*
* @param {string} proposal The proposal
*/
ExperimentalReportController.prototype.requestFilenameList = function(proposal) {
var _this = this;
if (proposal) {
// extract the proposal LetterCode (before the hyphen) and the digits (after
// the hyphen)
aroundHyphenRegExp = new RegExp(/^(.*)-(.*)/);
// extract the proposal LetterCode (before the hyphen) and the digits (after the hyphen)
var aroundHyphenRegExp = new RegExp(/^(.*)-(.*)/);
var beforeHyphen = aroundHyphenRegExp.exec(proposal)[1];
var afterHyphen = aroundHyphenRegExp.exec(proposal)[2];
......
{! Presentation of the metadata for the DOI which are from sessions !}
{@eq key=data.resourceType value=data.ES_RESOURCE_TYPE}
{@eq key=data.resourceType value=constants.ES_RESOURCE_TYPE}
<div class="row">
{?data.mergedProposalBeamlines}
{?data.mergedProposalBeamlines[0]}
......@@ -78,7 +78,7 @@
{! Presentation of the metadata for the DOI which are from dataset collections !}
{@eq key=data.resourceType value=data.DC_RESOURCE_TYPE}
{@eq key=data.resourceType value=constants.DC_RESOURCE_TYPE}
<div class="container-fluid">
<div class="row">
<div class="col-md-12 col-xl-8">
......
describe("Tests on doi controller", () => {
describe("getBeamlineUrl()", () => {
it("return beamlineurl for a 'ES' doi", () => {
let actualBeamlines = ['ID11'];
let actualDate = "2018-03-07"
let actualResourceType = CONSTANTS.ES_RESOURCE_TYPE;
let expectedBeamlineUrls = ["http://www.esrf.fr/UsersAndScience/Experiments/StructMaterials/ID11"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'ES' doi without date", () => {
let actualBeamlines = ['ID11'];
let actualDate = null;
let actualResourceType = CONSTANTS.ES_RESOURCE_TYPE;
let expectedBeamlineUrls = ["noLink"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'ES' doi which beamline does not exists", () => {
let actualBeamlines = ['ID11', 'ID100'];
let actualDate = "2018-03-07";
let actualResourceType = CONSTANTS.ES_RESOURCE_TYPE;
let expectedBeamlineUrls = ["http://www.esrf.fr/UsersAndScience/Experiments/StructMaterials/ID11", "noLink"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'ES' doi which beamline which exists but it does not exist at the provided date ", () => {
let actualBeamlines = ['ID11', 'ID100'];
let actualDate = "1900-01-01"; //ESRF was not built at this time
let actualResourceType = CONSTANTS.ES_RESOURCE_TYPE;
let expectedBeamlineUrls = ["noLink", "noLink"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'DC' doi", () => {
let actualBeamlines = ['ID11', 'ID21'];
let actualDate = "2018-03-07"
let actualResourceType = CONSTANTS.DC_RESOURCE_TYPE;
let expectedBeamlineUrls = ["http://www.esrf.fr/UsersAndScience/Experiments/StructMaterials/ID11", "http://www.esrf.eu/UsersAndScience/Experiments/XNP/ID21"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'DC' doi without date", () => {
let actualBeamlines = ['ID11', 'ID21'];
let actualDate = null
let actualResourceType = CONSTANTS.DC_RESOURCE_TYPE;
let expectedBeamlineUrls = ["noLink", "noLink"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'DC' doi which beamline does not exists", () => {
let actualBeamlines = ['ID11', 'ID100'];
let actualDate = "2018-03-07";
let actualResourceType = CONSTANTS.DC_RESOURCE_TYPE;
let expectedBeamlineUrls = ["http://www.esrf.fr/UsersAndScience/Experiments/StructMaterials/ID11", "noLink"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
it("return beamlineurl for a 'DC' doi which beamline which exists but it does not exist at the provided date ", () => {
let actualBeamlines = ['ID11', 'ID100'];
let actualDate = "1900-01-01"; //ESRF was not built at this time
let actualResourceType = CONSTANTS.DC_RESOURCE_TYPE;
let expectedBeamlineUrls = ["noLink", "noLink"];
myDoiController = new DOIController(null);
expect(myDoiController.getBeamlineUrl(actualBeamlines, actualDate, actualResourceType)).toEqual(expectedBeamlineUrls);
})
})
})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment