var assert = require('chai').assert;
var nodemock = require('nodemock');

var utils = require('./test-utils');

var express = require('express');
var expressKotoumi = require('../lib/index');
var Connection = require('../lib/backend-adaptor').Connection;

suite('REST API', function() {
  var connection;
  var sender;
  var receiver;
  var server;

  function commonSetup() {
    connection = new Connection({
      tag:        'test',
      listenPort: utils.testServerPort,
      sender:     sender = utils.createMockedSender(),
      receiver:   receiver = utils.createMockedReceiver()
    });
    receiver.triggerConnect('test');
  }

  function commonTeardown() {
    if (server) {
      server.close();
    }
    connection = undefined;
    sender = undefined;
    receiver = undefined;
    server = undefined;
  }

  test('registeration for given handlers', function() {
    var mockedHandlers = nodemock.mock('search')
          .takes('fake connection')
          .returns(function() {});
    var application = express();
    application.kotoumi({
      prefix:     '',
      connection: 'fake connection',
      handlers:   mockedHandlers
    });
    mockedHandlers.assertThrows();
  });

  suite('registeration', function() {
    setup(commonSetup);
    teardown(commonTeardown);

    function createHandlerFactory(type) {
      return function() {
        return function(request, response) {
          response.contentType('text/plain');
          response.send(type + ' OK', 200);
        };
      };
    }
    var handlersFactory = {
      search: createHandlerFactory('search')
    };

    test('to the document root', function(done) {
      var application = express();
      application.kotoumi({
        prefix:     '',
        connection: 'fake connection',
        handlers:   handlersFactory
      });
      server = utils.setupServer(application);

      utils
        .get('/tables/foobar')
        .next(function(response) {
          assert.equal('search OK', response.body);
          done();
        })
        .error(function(error) {
          done(error);
        });
    });

    test('under specified path', function(done) {
      var application = express();
      application.kotoumi({
        prefix:     '/path/to/kotoumi',
        connection: 'fake connection',
        handlers:   handlersFactory
      });
      server = utils.setupServer(application);

      utils
        .get('/path/to/kotoumi/tables/foobar')
        .next(function(response) {
          assert.equal('search OK', response.body);
          done();
        })
        .error(function(error) {
          done(error);
        });
    });
  });
});


@@ -284,544 +284,8 @@
 ) %7B%0A
-  var connection;%0A  var sender;%0A  var receiver;%0A  var server;%0A%0A  function commonSetup() %7B%0A    connection = new Connection(%7B%0A      tag:        'test',%0A      listenPort: utils.testServerPort,%0A      sender:     sender = utils.createMockedSender(),%0A      receiver:   receiver = utils.createMockedReceiver()%0A    %7D);%0A    receiver.triggerConnect('test');%0A  %7D%0A%0A  function commonTeardown() %7B%0A    if (server) %7B%0A      server.close();%0A    %7D%0A    connection = undefined;%0A    sender = undefined;%0A    receiver = undefined;%0A    server = undefined;%0A  %7D%0A%0A
   te
@@ -332,24 +332,24 @@
 unction() %7B%0A
+
     var mock
@@ -699,63 +699,8 @@
 ) %7B%0A
-    setup(commonSetup);%0A    teardown(commonTeardown);%0A%0A
     
@@ -1005,16 +1005,145 @@
     %7D;%0A%0A
+    var server;%0A    teardown(function() %7B%0A      if (server) %7B%0A        server.close();%0A      %7D%0A      server = undefined;%0A    %7D);%0A%0A
     test
@@ -2161,32 +2161,32 @@
 r);%0A        %7D);%0A
-
     %7D);%0A  %7D);%0A%7D)
@@ -2182,14 +2182,1127 @@
 );%0A  %7D);
+%0A%0A  test('creation of REST handler', function() %7B%0A    var requestBuilders = nodemock%0A          .mock('search')%0A            .takes(%7B request: true %7D)%0A            .returns(%7B requestMessage: true %7D);%0A    var responseBuilders = nodemock%0A          .mock('search')%0A            .takes(%7B responseMessage: true %7D)%0A            .returns(%7B response: true %7D);%0A%0A    var onReceive = %7B%7D;%0A    var connection = nodemock%0A          .mock('emitMessage')%0A            .takes('search', %7B requestMessage: true %7D, function() %7B%7D)%0A            .ctrl(2, onReceive);%0A    var handler = expressKotoumi%0A          .createRESTHandler(type,%0A                             requestBuilders,%0A                             responseBuilders,%0A                             connection);%0A%0A    var fakeRequest = %7B request: true %7D;%0A    var fakeResponse = nodemock%0A          .mock('contentType')%0A            .takes('application/json')%0A          .mock('send')%0A            .takes(%7B response: true %7D, 200);%0A%0A    handler(fakeRequest, fakeResponse);%0A    connection.assertThrows();%0A%0A    onReceive.trigger(%7B responseMessage: true %7D);%0A    fakeResponse.assertThrows();%0A  %7D);
 %0A%7D);%0A%0A

Add test for default handlers