import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import ElineService from '@/domain/services/Business/ElineService'
import {
  ELINE_GETALL_INPUT, ELINE_GETALL_OUTPUT,
  ELINE_ADD_CLIENT, ELINE_ADD_INPUT, ELINE_ADD_OUTPUT,
  ELINE_DELETE_CLIENT, ELINE_DELETE_INPUT, ELINE_DELETE_OUTPUT,
  ELINE_MODDIFY_CLIENT, ELINE_MODDIFY_INPUT, ELINE_MODDIFY_OUTPUT,
  ELINE_QUERY_CLIENT, ELINE_QUERY_INPUT, ELINE_QUERY_OUTPUT,
  TUNNEL_GETALL_INPUT,TUNNEL_GETALL_OUTPUT,
  SERVICETUNNEL_QUERY_CLIENT,SERVICETUNNEL_GETALL_INPUT,SERVICETUNNEL_GETALL_OUTPUT,
} from "../../../../mock/Business/ElineServiceMock";


describe('moxios', function () {
  it('should install', function () {
    let defaultAdapter = axios.defaults.adapter
    moxios.install()
    notEqual(axios.defaults.adapter, defaultAdapter)
    moxios.uninstall()
  })

  it('should uninstall', function () {
    let defaultAdapter = axios.defaults.adapter
    moxios.install()
    moxios.uninstall()
    equal(axios.defaults.adapter, defaultAdapter)
  })

  describe('ElineService (Rest)', function () {
    let onFulfilled
    let onRejected

    beforeEach(function () {
      moxios.install()
      // sinon.js 中 spy 主要用来监视函数的调用情况，
      // sinon 对待监视的函数进行 wrap 包装，
      // 因此可以通过它清楚的知道，该函数被调用过几次，传入什么参数返回什么结果，甚至是抛出的异常情况。
      onFulfilled = sinon.spy()
      onRejected = sinon.spy()
    })

    afterEach(function () {
      moxios.uninstall()
    })

    // 拦截请求
    it('should intercept requests', function (done) {
      ElineService.getAllTunnelCollection();
      ElineService.getAllServiceTunnelCollection(SERVICETUNNEL_QUERY_CLIENT);
      ElineService.getAllElineCollection();
      ElineService.addEline(ELINE_ADD_CLIENT);
      ElineService.deleteElines(ELINE_DELETE_CLIENT);
      ElineService.modifyEline(ELINE_MODDIFY_CLIENT);
      ElineService.conditionQueryEline(ELINE_QUERY_CLIENT);
      ElineService.getAllTunnelWidthCollection();
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 8);
        done()
      })
    });

    // 模拟响应
    it('should getAllTunnelCollection', function (done) {
      ElineService.getAllTunnelCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: TUNNEL_GETALL_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          // console.log(response);
          deepEqual(response, TUNNEL_GETALL_OUTPUT);
          done()
        })
      })
    });

    // 模拟响应
    it('should getAllServiceTunnelCollection', function (done) {
      ElineService.getAllServiceTunnelCollection(SERVICETUNNEL_QUERY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: SERVICETUNNEL_GETALL_INPUT
        }).then(function () {
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          deepEqual(response, SERVICETUNNEL_GETALL_OUTPUT);
          done()
        })
      })
    });

    // 模拟响应
    it('should getAllElineCollection', function (done) {
      ElineService.getAllElineCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINE_GETALL_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          // console.log(response);
          deepEqual(response, ELINE_GETALL_OUTPUT);
          done()
        })
      })
    });

    it('should addEline', function (done) {
      ElineService.addEline(ELINE_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINE_ADD_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINE_ADD_OUTPUT);
          done()
        })
      })
    });

    it('should deleteElines', function (done) {
      ElineService.deleteElines(ELINE_DELETE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINE_DELETE_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINE_DELETE_OUTPUT);
          done()
        })
      })
    });

    it('should modifyEline', function (done) {
      ElineService.modifyEline(ELINE_MODDIFY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINE_MODDIFY_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINE_MODDIFY_OUTPUT);
          done()
        })
      })
    });

    it('should conditionQueryEline', function (done) {
      ElineService.conditionQueryEline(ELINE_QUERY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ELINE_QUERY_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          //console.log(response);
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          deepEqual(response, ELINE_QUERY_OUTPUT);
          done()
        })
      })
    });

    // 模拟响应
    it('should getAllTunnelBandwidthCollection', function (done) {
      ElineService.getAllTunnelWidthCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: TUNNEL_GETALL_INPUT
        }).then(function () {
          // console.log(onFulfilled.getCall(0));
          let response = onFulfilled.getCall(0).args[0];
          equal(onFulfilled.called, true);
          // equal(response.status, 200);
          // console.log(response);
          deepEqual(response, TUNNEL_GETALL_OUTPUT);
          done()
        })
      })
    });


  })
});
