import { equal, notEqual, deepEqual,deepEql } from 'assert'
import axios from 'axios'
import moxios from 'moxios'
import sinon from 'sinon'
import UserGroupService from "../../../../src/domain/services/UserGroupService";
import {
  UG_ALLA_INPUT,
  UG_ALLA_OUTPUT,
  UG_ALLU_INPUT,
  UG_ALLU_OUTPUT
} from "../../../mock/deviceState/UserGroupServiceMock";
import UserService from "../../../../src/domain/services/UserService";
import {
  GETALL_USER_INPUT,
  GETALL_USER_OUTPUT,
  USER_ADD_CLIENT,
  USER_ADD_INPUT,
  USER_ADD_OUTPUT,
  USER_AUTH_INPUT,
  USER_AUTH_OUTPUT,
  USER_DEL_CLIENT,
  USER_DEL_INPUT,
  USER_DEL_OUTPUT,
  USER_GETUSERINFO_INPUT,
  USER_GETUSERINFO_OUTPUT,
  USER_MODI_CLIENT,
  USER_MODI_INPUT,
  USER_MODI_OUTPUT,
  USER_MODIINFO_CLIENT,
  USER_MODIINFO_INPUT,
  USER_MODIINFO_OUTPUT, USER_MODIPWD_INPUT,
  USER_MODIPWD_NEW,
  USER_MODIPWD_OLD, USER_MODIPWD_OUTPUT,
  USER_MODIPWD_USER,
  USER_QUERY_CLIENT,
  USER_QUERY_INPUT,
  USER_QUERY_OUTPUT,
  USER_RES_CLIENT,
  USER_RES_INPUT,
  USER_RES_OUTPUT
} from "../../../mock/deviceState/UserServiceMock";

describe('UserService_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('UserService (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) {
      UserService.getAllUserCollection();
      UserService.addUser(USER_ADD_CLIENT);
      //UserGroupService.createUserGroup(SUBNET_DELETE_CLIENT);
      //UserGroupService.deleteUserGroup(SUBNET_MODDIFY_CLIENT);
      //UserGroupService.updateUserGroup(SUBNET_QUERY_CLIENT);

      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 2);
        done()
      })
    });

    // 模拟响应
    it('should getAllUserCollection', function (done) {
      UserService.getAllUserCollection().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: GETALL_USER_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, GETALL_USER_OUTPUT);
          done()
        })
      })
    });

    it('should addUser', function (done) {
      UserService.addUser(USER_ADD_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_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, USER_ADD_OUTPUT);
          done()
        })
      })
    });

    it('should deleteUser', function (done) {
      UserService.deleteUser(USER_DEL_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_DEL_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, USER_DEL_OUTPUT);
          done()
        })
      })
    });

    it('should modifyUser', function (done) {
      UserService.modifyUser(USER_MODI_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_MODI_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, USER_MODI_OUTPUT);
          done()
        })
      })
    });

    it('should conditionQueryUser', function (done) {
      UserService.conditionQueryUser(USER_QUERY_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_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, USER_QUERY_OUTPUT);
          done()
        })
      })
    });

    it('should resetUserPassword', function (done) {
      UserService.resetUserPassword(USER_RES_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_RES_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, USER_RES_OUTPUT);
          done()
        })
      })
    });

    it('should getAllUserAuthority', function (done) {
      UserService.getAllUserAuthority().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_AUTH_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, USER_AUTH_OUTPUT);
          done()
        })
      })
    });

    it('should getUserInfo', function (done) {
      UserService.getUserInfo().then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_GETUSERINFO_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, USER_GETUSERINFO_OUTPUT);
          done()
        })
      })
    });

    it('should modifyUserInfo', function (done) {
      UserService.modifyUserInfo(USER_MODIINFO_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_MODIINFO_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, USER_MODIINFO_OUTPUT);
          done()
        })
      })
    });

    it('should modifyUserPwd', function (done) {
      UserService.modifyUserPwd(USER_MODIPWD_OLD,USER_MODIPWD_NEW,USER_MODIPWD_USER).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: USER_MODIPWD_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, USER_MODIPWD_OUTPUT);
          done()
        })
      })
    });



  })
});
