"use strict";

/*
*  test router to exercise all tests uning the restbase-cassandra handler
*/

var RouteSwitch = require('routeswitch');

function setupConfigDefaults(conf) {
    if (!conf) { conf = {
            // module name
            type: "restbase-cassandra",
            hosts: ["localhost"],
            keyspace: "system",
            username: "cassandra",
            password: "cassandra",
            defaultConsistency: 'one',
            salt_key: "secret",
            maxLimit: 250
        };
    }
    return conf;
}

var router = {};
router.request = function(req) {
	var match = this.newRouter.match(req.url);
	if (match) {
		req.params = match.params;
		var handler = match.methods[req.method.toLowerCase()];
		if (handler && handler.request_handler) {
			return handler.request_handler({}, req)
			.then(function(item){
				return item;
			});
		}
	}
};

router.makeRouter = function(req) {
	var self = this;
	var conf = setupConfigDefaults();
	var opt = {
		log: function(){},
		conf: conf
	};

	return require('../index.js')(opt)
	.then(function(handler) {
		self.newRouter = new RouteSwitch.fromHandlers([handler]);
		return self;
	})
	.catch(function(e) {
		console.log('Error during RESTBase startup: ', e);
	});
};

module.exports = router;

@@ -578,25 +578,28 @@
 tion(req) %7B%0A
-%09
+    
 var match = 
@@ -629,17 +629,20 @@
 q.url);%0A
-%09
+    
 if (matc
@@ -646,18 +646,24 @@
 atch) %7B%0A
-%09%09
+        
 req.para
@@ -681,18 +681,24 @@
 params;%0A
-%09%09
+        
 var hand
@@ -744,18 +744,24 @@
 ase()%5D;%0A
-%09%09
+        
 if (hand
@@ -767,140 +767,922 @@
 dler
- && handler.request_handler) %7B%0A%09%09%09return handler.request_handler(%7B%7D, req)%0A%09%09%09.then(function(item)%7B%0A%09%09%09%09return item;%0A%09%09%09%7D);%0A%09%09%7D%0A%09%7D
+) %7B%0A            return handler(%7B%7D, req)%0A            .then(function(item)%7B%0A                return item;%0A            %7D);%0A        %7D else %7B%0A            throw new Error('No handler for ' + req.method + ' ' + req.url);%0A        %7D%0A    %7D else %7B%0A        throw new Error('No match for ' + req.method + ' ' + req.url);%0A    %7D%0A%7D;%0A%0Afunction flatHandlerFromModDef (modDef, prefix) %7B%0A    var handler = %7B paths: %7B%7D %7D;%0A    Object.keys(modDef.spec.paths).forEach(function(path) %7B%0A        var pathModSpec = modDef.spec.paths%5Bpath%5D;%0A        handler.paths%5Bprefix + path%5D = %7B%7D;%0A        Object.keys(pathModSpec).forEach(function(m) %7B%0A            var opId = pathModSpec%5Bm%5D.operationId;%0A            if (!modDef.operations%5BopId%5D) %7B%0A                throw new Error('The module does not export the opration ' + opId);%0A            %7D%0A            handler.paths%5Bprefix + path%5D%5Bm%5D = modDef.operations%5BopId%5D;%0A        %7D);%0A    %7D);%0A    return handler;
 %0A%7D
-;
 %0A%0Aro
@@ -1715,17 +1715,20 @@
 (req) %7B%0A
-%09
+    
 var self
@@ -1736,17 +1736,20 @@
 = this;%0A
-%09
+    
 var conf
@@ -1774,17 +1774,20 @@
 ults();%0A
-%09
+    
 var opt 
@@ -1790,18 +1790,24 @@
 opt = %7B%0A
-%09%09
+        
 log: fun
@@ -1817,18 +1817,24 @@
 on()%7B%7D,%0A
-%09%09
+        
 conf: co
@@ -1840,14 +1840,20 @@
 onf%0A
-%09%7D;%0A%0A%09
+    %7D;%0A%0A    
 retu
@@ -1883,17 +1883,20 @@
 ')(opt)%0A
-%09
+    
 .then(fu
@@ -1906,21 +1906,26 @@
 ion(
-handler) %7B%0A%09%09
+modDef) %7B%0A        
 self
@@ -1971,21 +1971,66 @@
 rs(%5B
-h
+flatH
 andler
-%5D);%0A%09%09
+FromModDef(modDef, '/%7Bdomain%7D/sys')%5D);%0A        
 retu
@@ -2042,13 +2042,19 @@
 lf;%0A
-%09%7D)%0A%09
+    %7D)%0A    
 .cat
@@ -2074,10 +2074,16 @@
 ) %7B%0A
-%09%09
+        
 cons
@@ -2133,9 +2133,12 @@
 e);%0A
-%09
+    
 %7D);%0A

Test router: turn module exported object into a routeswitch handler