'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

require('source-map-support/register');

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _nodeStatic = require('node-static');

var _nodeStatic2 = _interopRequireDefault(_nodeStatic);

var _sinon = require('sinon');

var _sinon2 = _interopRequireDefault(_sinon);

var _mocha = require('mocha');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _Executor = require('../../src/agent/Executor');

var _ExecutorTeamCity = require('../../src/executors/ExecutorTeamCity');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _mocha.describe)('ExecutorTeamCity', function () {
    var _this = this;

    var executorTeamcity = void 0;
    var eventData = void 0;
    process.env.GTM_TEAMCITY_USER = 'admin';
    process.env.GTM_TEAMCITY_PASSCODE = 'admin';
    process.env.GTM_TEAMCITY_URL = 'http://localhost:8111';

    (0, _mocha.beforeEach)(function () {
        eventData = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/executorTeamCityTaskPayLoad.json', 'utf-8'));
        executorTeamcity = new _ExecutorTeamCity.ExecutorTeamCity(eventData, console);
    });

    (0, _mocha.describe)('constructor', function () {
        (0, _mocha.it)('should instantiate as Executor', function () {
            _assert2.default.equal(executorTeamcity instanceof _Executor.Executor, true);
        });
    });

    (0, _mocha.describe)('createTeamCityBuildNode', function () {
        eventData = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/executorTeamCityTaskPayLoad.json', 'utf-8'));
        var jobName = eventData.ghTaskConfig.task.context;

        var buildNodeObject = '<build>' + '<buildType id="' + jobName + '"/>' + '<properties>' + '<property name="build_tag" value="proj_wasX"/>' + '<property name="cuke_env" value="env_dev"/>' + '<property name="cuke_tags" value="@proj_wasX,@proj_wasn"/>' + '</properties>' + '</build>';

        (0, _mocha.it)('should return build request xml payload', function () {
            _assert2.default.equal(executorTeamcity.createTeamCityBuildNode(eventData.ghTaskConfig.task, jobName), buildNodeObject);
        });
    });

    (0, _mocha.describe)('executeTask', function () {
        eventData = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/executorTeamCityTaskPayLoad.json', 'utf-8'));

        var stubCall = void 0;
        var customResult = void 0;
        (0, _mocha.before)(function () {
            customResult = { content: 'counterfeit task executed' };
            stubCall = _sinon2.default.stub(executorTeamcity, 'executeTask').returns(_promise2.default.resolve(customResult));
        });

        (0, _mocha.it)('executeTask to return the custom result object', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            var result;
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            _context.next = 2;
                            return stubCall(eventData.ghTaskConfig.task);

                        case 2:
                            result = _context.sent;

                            _assert2.default.equal(result, customResult);

                        case 4:
                        case 'end':
                            return _context.stop();
                    }
                }
            }, _callee, _this);
        })));

        (0, _mocha.after)(function () {
            stubCall.restore();
        });

        (0, _mocha.it)('execute Task to return NO_MATCHING_TASK for invalid jobName', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var sampleTask, result;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            sampleTask = {};

                            sampleTask.options = { executor: 'TeamCity', build_tag: 'proj_wasX', cuke_env: 'env_dev' };
                            _context2.next = 4;
                            return executorTeamcity.executeTask(sampleTask);

                        case 4:
                            result = _context2.sent;

                            _assert2.default.equal(result, 'NO_MATCHING_TASK');

                        case 6:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, _this);
        })));

        (0, _mocha.it)('executeTask to return result object', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            eventData = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/executorTeamCityTaskPayLoad.json', 'utf-8'));
                            _context3.prev = 1;
                            _context3.next = 4;
                            return executorTeamcity.executeTask(eventData.ghTaskConfig.task);

                        case 4:
                            _context3.next = 9;
                            break;

                        case 6:
                            _context3.prev = 6;
                            _context3.t0 = _context3['catch'](1);
                            return _context3.abrupt('return', _assert2.default.equal(_context3.t0.message, 'connect ECONNREFUSED 127.0.0.1:8111'));

                        case 9:
                        case 'end':
                            return _context3.stop();
                    }
                }
            }, _callee3, _this, [[1, 6]]);
        })));
    });

    (0, _mocha.describe)('createResultObject', function () {
        var expectedResult = {};
        expectedResult.TotalTestCount = 18;
        expectedResult.PassedTestCount = 17;
        expectedResult.FailedTestCount = 1;
        expectedResult.testResultsUrl = undefined;

        (0, _mocha.it)('converts xml response of TC build to json', function (done) {
            _fs2.default.readFileSync(__dirname + '/../fixtures/teamCityStatisticsXMLPayload.xml', function (data) {
                _assert2.default.equal(executorTeamcity.createResultObject(data), expectedResult);
            });
            done();
        });
    });

    (0, _mocha.describe)('getBuildStatistics', function () {
        var staticServer = void 0;
        var returnXMLContext = void 0;
        (0, _mocha.before)(function () {
            var file = new _nodeStatic2.default.Server(__dirname + '/../fixtures/teamCityStatisticsXMLPayload.xml');
            returnXMLContext = _fs2.default.readFileSync(__dirname + '/../fixtures/teamCityStatisticsXMLPayload.xml', function (data) {
                return data;
            });

            staticServer = require('http').createServer(function (request, response) {
                file.serve(request, response);
            });

            staticServer.listen(4321);
        });

        var statisticsUrl = 'http://localhost:4321';

        (0, _mocha.it)('reads the statistics xml payload from static server', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
            var resultXML;
            return _regenerator2.default.wrap(function _callee4$(_context4) {
                while (1) {
                    switch (_context4.prev = _context4.next) {
                        case 0:
                            _context4.next = 2;
                            return executorTeamcity.getBuildStatistics(statisticsUrl);

                        case 2:
                            resultXML = _context4.sent;

                            _assert2.default.equal(resultXML, returnXMLContext);

                        case 4:
                        case 'end':
                            return _context4.stop();
                    }
                }
            }, _callee4, _this);
        })));

        (0, _mocha.after)(function () {
            staticServer.close();
        });
    });

    (0, _mocha.describe)('waitForBuildToComplete', function () {
        var customResult = {};
        customResult.state = 'finished';

        var stubCall = void 0;
        (0, _mocha.before)(function () {
            stubCall = _sinon2.default.stub(executorTeamcity, 'waitForBuildToComplete').returns(_promise2.default.resolve(customResult));
        });

        (0, _mocha.it)('return result for custom build status set as finished', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() {
            var buildNumber, buildName, result;
            return _regenerator2.default.wrap(function _callee5$(_context5) {
                while (1) {
                    switch (_context5.prev = _context5.next) {
                        case 0:
                            buildNumber = 12;
                            buildName = 'projectName';
                            _context5.next = 4;
                            return stubCall(buildName, buildNumber);

                        case 4:
                            result = _context5.sent;

                            _assert2.default.equal(result, customResult);

                        case 6:
                        case 'end':
                            return _context5.stop();
                    }
                }
            }, _callee5, _this);
        })));

        (0, _mocha.after)(function () {
            stubCall.restore();
        });

        (0, _mocha.it)('waits for build to complete with finished state', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() {
            var invalidBuild, buildNumber, buildName;
            return _regenerator2.default.wrap(function _callee6$(_context6) {
                while (1) {
                    switch (_context6.prev = _context6.next) {
                        case 0:
                            invalidBuild = {};

                            invalidBuild.message = 'Invalid Build Information';
                            buildNumber = 12;
                            buildName = 'projectName';
                            _context6.prev = 4;
                            _context6.next = 7;
                            return executorTeamcity.waitForBuildToComplete(buildName, buildNumber);

                        case 7:
                            _context6.next = 12;
                            break;

                        case 9:
                            _context6.prev = 9;
                            _context6.t0 = _context6['catch'](4);
                            return _context6.abrupt('return', _assert2.default.equal(_context6.t0.message, 'connect ECONNREFUSED 127.0.0.1:8111'));

                        case 12:
                        case 'end':
                            return _context6.stop();
                    }
                }
            }, _callee6, _this, [[4, 9]]);
        })));
    });
});