Named maps adapter with getMapConfig interface

This commit is contained in:
Raul Ochoa 2016-05-23 15:39:11 +02:00
parent 3e35604df0
commit 9f6ce64a31
5 changed files with 39 additions and 37 deletions

View File

@ -175,15 +175,12 @@ MapController.prototype.create = function(req, res, prepareConfigFn) {
assert.ifError(err); assert.ifError(err);
var next = this; var next = this;
analysesResults = _analysesResults; analysesResults = _analysesResults;
self.namedLayersAdapter.getLayers(req.context.user, requestMapConfig.layers, self.pgConnection, self.namedLayersAdapter.getMapConfig(req.context.user, requestMapConfig, self.pgConnection,
function(err, layers, datasource) { function(err, requestMapConfig, datasource) {
if (err) { if (err) {
return next(err); return next(err);
} }
if (layers) {
requestMapConfig.layers = layers;
}
return next(null, requestMapConfig, datasource); return next(null, requestMapConfig, datasource);
} }
); );

View File

@ -8,11 +8,13 @@ function MapConfigNamedLayersAdapter(templateMaps) {
module.exports = MapConfigNamedLayersAdapter; module.exports = MapConfigNamedLayersAdapter;
MapConfigNamedLayersAdapter.prototype.getLayers = function(username, layers, dbMetadata, callback) { MapConfigNamedLayersAdapter.prototype.getMapConfig = function(username, requestMapConfig, dbMetadata, callback) {
var self = this; var self = this;
var layers = requestMapConfig.layers;
if (!layers) { if (!layers) {
return callback(null); return callback(null, requestMapConfig);
} }
var adaptLayersQueue = queue(layers.length); var adaptLayersQueue = queue(layers.length);
@ -96,7 +98,9 @@ MapConfigNamedLayersAdapter.prototype.getLayers = function(username, layers, dbM
}); });
return callback(null, layers, datasourceBuilder.build()); requestMapConfig.layers = layers;
return callback(null, requestMapConfig, datasourceBuilder.build());
} }
@ -114,7 +118,7 @@ MapConfigNamedLayersAdapter.prototype.getLayers = function(username, layers, dbM
adaptLayersQueue.awaitAll(layersAdaptQueueFinish); adaptLayersQueue.awaitAll(layersAdaptQueueFinish);
}); });
} else { } else {
return callback(null, layers, datasourceBuilder.build()); return callback(null, requestMapConfig, datasourceBuilder.build());
} }
}; };

View File

@ -131,15 +131,12 @@ NamedMapMapConfigProvider.prototype.getMapConfig = function(callback) {
assert.ifError(err); assert.ifError(err);
var next = this; var next = this;
self.analysesResults = analysesResults || []; self.analysesResults = analysesResults || [];
self.namedLayersAdapter.getLayers(self.owner, _mapConfig.layers, self.pgConnection, self.namedLayersAdapter.getMapConfig(self.owner, _mapConfig, self.pgConnection,
function(err, layers, datasource) { function(err, _mapConfig, datasource) {
if (err) { if (err) {
return next(err); return next(err);
} }
if (layers) {
_mapConfig.layers = layers;
}
return next(null, _mapConfig, datasource); return next(null, _mapConfig, datasource);
} }
); );

View File

@ -294,9 +294,9 @@ describe('named_layers datasources', function() {
testScenarios.forEach(function(testScenario) { testScenarios.forEach(function(testScenario) {
it('should return a list of layers ' + testScenario.desc, function(done) { it('should return a list of layers ' + testScenario.desc, function(done) {
mapConfigNamedLayersAdapter.getLayers(username, testScenario.config.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, testScenario.config, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
testScenario.test(err, layers, datasource, done); testScenario.test(err, mapConfig.layers, datasource, done);
} }
); );
}); });

View File

@ -147,10 +147,10 @@ describe('mapconfig-named-layers-adapter', function() {
var missingNamedMapLayerConfig = makeNamedMapLayerConfig({ var missingNamedMapLayerConfig = makeNamedMapLayerConfig({
config: {} config: {}
}); });
mapConfigNamedLayersAdapter.getLayers(username, missingNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, missingNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(err); assert.ok(err);
assert.ok(!layers); assert.ok(!mapConfig);
assert.ok(!datasource); assert.ok(!datasource);
assert.equal(err.message, 'Missing Named Map `name` in layer options'); assert.equal(err.message, 'Missing Named Map `name` in layer options');
@ -164,10 +164,10 @@ describe('mapconfig-named-layers-adapter', function() {
var nonExistentNamedMapLayerConfig = makeNamedMapLayerConfig({ var nonExistentNamedMapLayerConfig = makeNamedMapLayerConfig({
name: missingTemplateName name: missingTemplateName
}); });
mapConfigNamedLayersAdapter.getLayers(username, nonExistentNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, nonExistentNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(err); assert.ok(err);
assert.ok(!layers); assert.ok(!mapConfig);
assert.ok(!datasource); assert.ok(!datasource);
assert.equal( assert.equal(
err.message, "Template '" + missingTemplateName + "' of user '" + username + "' not found" err.message, "Template '" + missingTemplateName + "' of user '" + username + "' not found"
@ -187,10 +187,10 @@ describe('mapconfig-named-layers-adapter', function() {
var nonAuthTokensNamedMapLayerConfig = makeNamedMapLayerConfig({ var nonAuthTokensNamedMapLayerConfig = makeNamedMapLayerConfig({
name: tokenAuthTemplateName name: tokenAuthTemplateName
}); });
mapConfigNamedLayersAdapter.getLayers(username, nonAuthTokensNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, nonAuthTokensNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(err); assert.ok(err);
assert.ok(!layers); assert.ok(!mapConfig);
assert.ok(!datasource); assert.ok(!datasource);
assert.equal(err.message, "Unauthorized '" + tokenAuthTemplateName + "' template instantiation"); assert.equal(err.message, "Unauthorized '" + tokenAuthTemplateName + "' template instantiation");
@ -209,10 +209,10 @@ describe('mapconfig-named-layers-adapter', function() {
var nestedNamedMapLayerConfig = makeNamedMapLayerConfig({ var nestedNamedMapLayerConfig = makeNamedMapLayerConfig({
name: nestedNamedMapTemplateName name: nestedNamedMapTemplateName
}); });
mapConfigNamedLayersAdapter.getLayers(username, nestedNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, nestedNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(err); assert.ok(err);
assert.ok(!layers); assert.ok(!mapConfig);
assert.ok(!datasource); assert.ok(!datasource);
assert.equal(err.message, 'Nested named layers are not allowed'); assert.equal(err.message, 'Nested named layers are not allowed');
@ -226,9 +226,10 @@ describe('mapconfig-named-layers-adapter', function() {
var validNamedMapMapLayerConfig = makeNamedMapLayerConfig({ var validNamedMapMapLayerConfig = makeNamedMapLayerConfig({
name: templateName name: templateName
}); });
mapConfigNamedLayersAdapter.getLayers(username, validNamedMapMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, validNamedMapMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(!err); assert.ok(!err);
var layers = mapConfig.layers;
assert.ok(layers.length, 1); assert.ok(layers.length, 1);
assert.ok(layers[0].type, 'cartodb'); assert.ok(layers[0].type, 'cartodb');
assert.notEqual(datasource.getLayerDatasource(0), undefined); assert.notEqual(datasource.getLayerDatasource(0), undefined);
@ -248,9 +249,10 @@ describe('mapconfig-named-layers-adapter', function() {
name: tokenAuthTemplateName, name: tokenAuthTemplateName,
auth_tokens: ['valid1'] auth_tokens: ['valid1']
}); });
mapConfigNamedLayersAdapter.getLayers(username, validAuthTokensNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, validAuthTokensNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(!err); assert.ok(!err);
var layers = mapConfig.layers;
assert.equal(layers.length, 1); assert.equal(layers.length, 1);
assert.notEqual(datasource.getLayerDatasource(0), undefined); assert.notEqual(datasource.getLayerDatasource(0), undefined);
@ -270,9 +272,10 @@ describe('mapconfig-named-layers-adapter', function() {
name: multipleLayersTemplateName, name: multipleLayersTemplateName,
auth_tokens: ['valid2'] auth_tokens: ['valid2']
}); });
mapConfigNamedLayersAdapter.getLayers(username, multipleLayersNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(!err); assert.ok(!err);
var layers = mapConfig.layers;
assert.equal(layers.length, 2); assert.equal(layers.length, 2);
assert.equal(layers[0].type, 'mapnik'); assert.equal(layers[0].type, 'mapnik');
@ -306,9 +309,10 @@ describe('mapconfig-named-layers-adapter', function() {
}, },
auth_tokens: ['valid2'] auth_tokens: ['valid2']
}); });
mapConfigNamedLayersAdapter.getLayers(username, multipleLayersNamedMapLayerConfig.layers, pgConnection, mapConfigNamedLayersAdapter.getMapConfig(username, multipleLayersNamedMapLayerConfig, pgConnection,
function(err, layers, datasource) { function(err, mapConfig, datasource) {
assert.ok(!err); assert.ok(!err);
var layers = mapConfig.layers;
assert.equal(layers.length, 2); assert.equal(layers.length, 2);
assert.equal(layers[0].type, 'mapnik'); assert.equal(layers[0].type, 'mapnik');