'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 _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 _ExecutorJenkins = require('../../src/executors/ExecutorJenkins');

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

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

    var executorJenkins = void 0;
    var eventData = void 0;
    process.env.GTM_JENKINS_USER = 'ciuser';
    process.env.GTM_JENKINS_URL = 'http://localhost:8211';
    var NON_EXISTING_JENKINS_SERVER = ': connect ECONNREFUSED 127.0.0.1:8211';

    (0, _mocha.beforeEach)(function () {
        eventData = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/executorJenkinsTaskPayload.json', 'utf-8'));
        executorJenkins = new _ExecutorJenkins.ExecutorJenkins(eventData, console);
    });

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

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

        (0, _mocha.it)('should return the run 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)('executeTask to return result object', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            eventData = JSON.parse(_fs2.default.readFileSync(__dirname + '/../fixtures/executorJenkinsTaskPayload.json', 'utf-8'));
                            _context2.prev = 1;
                            _context2.next = 4;
                            return executorJenkins.executeTask(eventData.ghTaskConfig.tasks).then(function (data) {
                                return data;
                            });

                        case 4:
                            _context2.next = 9;
                            break;

                        case 6:
                            _context2.prev = 6;
                            _context2.t0 = _context2['catch'](1);
                            return _context2.abrupt('return', _assert2.default.equal(_context2.t0.message, 'jenkins: job.build' + NON_EXISTING_JENKINS_SERVER));

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

    (0, _mocha.describe)('waitForBuild', function () {
        var customResult = {};
        customResult.state = 'finished';
        var buildNumber = 12;
        var buildName = 'projectName';
        var stubCall = void 0;
        (0, _mocha.before)(function () {
            stubCall = _sinon2.default.stub(executorJenkins, 'waitForBuild').returns(_promise2.default.resolve(customResult));
        });

        (0, _mocha.it)('should wait for build to complete(mocked)', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            var result;
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            _context3.next = 2;
                            return stubCall(buildName, buildNumber);

                        case 2:
                            result = _context3.sent;

                            _assert2.default.equal(result, customResult);

                        case 4:
                        case 'end':
                            return _context3.stop();
                    }
                }
            }, _callee3, _this);
        })));

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

        (0, _mocha.it)('should throw error while waiting for non-existing build', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
            return _regenerator2.default.wrap(function _callee4$(_context4) {
                while (1) {
                    switch (_context4.prev = _context4.next) {
                        case 0:
                            _context4.prev = 0;
                            _context4.next = 3;
                            return executorJenkins.waitForBuild(buildName, buildNumber).then(function (data) {
                                return data;
                            });

                        case 3:
                            _context4.next = 8;
                            break;

                        case 5:
                            _context4.prev = 5;
                            _context4.t0 = _context4['catch'](0);
                            return _context4.abrupt('return', _assert2.default.equal(_context4.t0.message, 'jenkins: build.get' + NON_EXISTING_JENKINS_SERVER));

                        case 8:
                        case 'end':
                            return _context4.stop();
                    }
                }
            }, _callee4, _this, [[0, 5]]);
        })));
    });

    (0, _mocha.describe)('waitForBuildToExist', function () {
        var customResult = true;

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

        (0, _mocha.it)('should wait for the build to complete', (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.describe)('buildNumberfromQueue', function () {
        (0, _mocha.it)('should return build number from queue', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() {
            var result;
            return _regenerator2.default.wrap(function _callee6$(_context6) {
                while (1) {
                    switch (_context6.prev = _context6.next) {
                        case 0:
                            _context6.prev = 0;
                            _context6.next = 3;
                            return executorJenkins.buildNumberfromQueue(12);

                        case 3:
                            result = _context6.sent;

                            console.log(result);
                            _context6.next = 10;
                            break;

                        case 7:
                            _context6.prev = 7;
                            _context6.t0 = _context6['catch'](0);
                            return _context6.abrupt('return', _assert2.default.equal(_context6.t0.message, 'jenkins: queue.item' + NON_EXISTING_JENKINS_SERVER));

                        case 10:
                        case 'end':
                            return _context6.stop();
                    }
                }
            }, _callee6, _this, [[0, 7]]);
        })));
    });
});