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

var temporaryDatabase;

suiteSetup(function() {
  temporaryDatabase = utils.createTemporaryDatabase();
});

suiteTeardown(function() {
  temporaryDatabase.teardown();
  temporaryDatabase = undefined;
});

suite('Configuration API', function() {
  var database;
  var server;

  setup(function() {
    database = temporaryDatabase.get();
    server = utils.setupServer(database);
  });

  teardown(function() {
    server.close();
    temporaryDatabase.clear();
  });

  test('Get, Action=CreateDomain', function(done) {
    var path = '/?DomainName=companies&Action=CreateDomain&Version=2011-02-01';
    utils.get(path)
      .next(function(response) {
        var expected = {
              statusCode: 200,
              body: ''
            };
//        assert.deepEqual(response, expected);

        var dump = database.commandSync('dump', {
              tables: 'companies'
            });
        var expected = 'table_create companies TABLE_HASH_KEY ShortText\n' +
                       'table_create companies_BigramTerms ' +
                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
                         '--default_tokenizer TokenBigram';
        assert.equal(dump, expected);

        done();
      })
      .error(function(error) {
        done(error);
      });
  });

  test('Get, Action=DefineIndexField', function(done) {
    var path = '/?DomainName=companies&Action=CreateDomain&Version=2011-02-01';
    utils.get(path)
      .next(function(response) {
        var path = '/?DomainName=companies&IndexField.IndexFieldName=name&' +
                   'Action=DefineIndexField&Version=2011-02-01';
        return utils.get(path);
      })
      .next(function(response) {
        var expected = {
              statusCode: 200,
              body: ''
            };
//        assert.deepEqual(response, expected);

        var dump = database.commandSync('dump', {
              tables: 'companies'
            });
        var expected = 'table_create companies_BigramTerms ' +
                         'TABLE_PAT_KEY|KEY_NORMALIZE ShortText ' +
                         '--default_tokenizer TokenBigram\n' +
                       'table_create companies TABLE_HASH_KEY ShortText\n' +
                       'column_create companies name COLUMN_SCALAR ShortText\n' +
                       'column_create companies_BigramTerms companies_name ' +
                         'COLUMN_INDEX|WITH_POSITION companies name';
        assert.equal(dump, expected);

        done();
      })
      .error(function(error) {
        done(error);
      });
  });

  test('Get, no version', function(done) {
    var path = '/';
    utils.get(path)
      .next(function(response) {
        var expected = {
              statusCode: 400,
              body: 'API version must be given as the parameter "Version".'
            };
        assert.deepEqual(response, expected);
        done();
      })
      .error(function(error) {
        done(error);
      });
  });

  test('Get, invalid version', function(done) {
    var path = '/?Version=2011-02-02';
    utils.get(path)
      .next(function(response) {
        var expected = {
              statusCode: 400,
              body: 'API version "2011-02-02" is not supported.'
            };
        assert.deepEqual(response, expected);
        done();
      })
      .error(function(error) {
        done(error);
      });
  });

  test('Get, no action', function(done) {
    var path = '/?Version=2011-02-01';
    utils.get(path)
      .next(function(response) {
        var expected = {
              statusCode: 400,
              body: 'Action must be given as the parameter "Action".'
            };
        assert.deepEqual(response, expected);
        done();
      })
      .error(function(error) {
        done(error);
      });
  });

  test('Get, invalid action', function(done) {
    var path = '/?Version=2011-02-01&Action=unknown';
    utils.get(path)
      .next(function(response) {
        var expected = {
              statusCode: 400,
              body: 'Action "unknown" is not supported.'
            };
        assert.deepEqual(response, expected);
        done();
      })
      .error(function(error) {
        done(error);
      });
  });
});

@@ -293,24 +293,381 @@
 fined;%0A%7D);%0A%0A
+function createCommonErrorResponse(errorCode, message) %7B%0A  return '%3C?xml version=%221.0%22?%3E%5Cn' +%0A         '%3CResponse%3E' +%0A           '%3CErrors%3E' +%0A             '%3CError%3E%3CCode%3E' + errorCode +'%3C/Code%3E' +%0A                     '%3CMessage%3E' + message + '%3C/Message%3E%3C/Error%3E' +%0A             '%3C/Errors%3E' +%0A           '%3CRequestID%3E%3C/RequestID%3E' +%0A         '%3C/Response%3E';%0A%7D%0A%0A
 suite('Confi
@@ -3975,32 +3975,114 @@
 ion(response) %7B%0A
+        var message = 'The request is missing an action or operation parameter.';%0A
         var expe
@@ -4145,57 +4145,59 @@
 dy: 
-'Action must be given as the parameter %22Action%22.'
+createCommonErrorResponse('InvalidAction', message)
 %0A   
@@ -4494,32 +4494,111 @@
 ion(response) %7B%0A
+        var message = 'The action unknown is not valid for this web service.';%0A
         var expe
@@ -4661,44 +4661,59 @@
 dy: 
-'Action %22unknown%22 is not supported.'
+createCommonErrorResponse('InvalidAction', message)
 %0A   

Update tests for XML repsonses