var base = require('./base');
var nock = require('nock');
var fs = require('fs');
var path = require('path');

var xml_fixture = fs.readFileSync(path.join(__dirname,'fixtures', 'version_one.xml'));

exports.test_example_minimum = function(test, assert) {
  base.run_test(test, assert, 'example-minimum.json', 'version_one', 'version_one', _success_mock, false);
};

exports.test_example_simple = function(test, assert) {
  base.run_test(test, assert, 'example-simple.json', 'version_one', 'version_one', _success_mock, false);
};

exports.test_example_full_success = function(test, assert) {
  base.run_test(test, assert, 'example-full.json', 'version_one', 'version_one', _success_mock, true);
};

exports.test_example_full_error = function(test, assert) {
  base.run_test(test, assert, 'example-full.json', 'version_one', 'version_one', _error_mock, true, true);
};


function _create_mock(req, status, res) {
  var mock;
  var selection = ["CreateDate",
                  "Owners",
                  "SecurityScope",
                  "AssetState",
                  "Owners",
                  "AssetType",
                  "Status",
                  "Number",
                  "Order",
                  "Description",
                  "Scope.Name",
                  "Name",
                  "ResolutionReason",
                  "Timebox",
                  "Resolution",
                  "Scope",
                  "Priority"];

  if (!req.devops.related_apis || !req.devops.related_apis.version_one) {
    mock = null;
  } else {
    mock = nock(
      [{80: 'http', 443: 'https'}[req.devops.related_apis.version_one.port],
       '://',
       req.devops.related_apis.version_one.host].join(''));
    mock = mock.get(
        ['/',
        req.devops.related_apis.version_one.name,
        "/rest-1.v1/Data/Defect?sel=",
        selection,
        "&where=AssetState='0','64';Scope='Scope:",
        req.devops.related_apis.version_one.project,
        "'"].join(""));
    mock = mock.reply(status, res);
    //mock = mock.log(console.log);
  }
  return mock;
}

function _success_mock(req) {
  return _create_mock(
    req,
    200,
    xml_fixture);
}

function _error_mock(req) {
  // intentionall provide invalid XML
  return _create_mock(
    req,
    500,
    "{'error': {'message': 'API returned error', 'code': 22}}");
}
@@ -936,16 +936,89 @@
 tion = %5B
+%22Custom_Severity.Name%22,%0A                  %22ChangedBy%22,%0A                  
 %22CreateD
@@ -2091,14 +2091,14 @@
 s);%0A
+//
     
-//
 mock

Match the right url