import axios from "axios";
import moxios from "moxios";
import {deepEqual, equal, notEqual} from "assert";
import sinon from "sinon";
import AlarmManagerService from "../../../../src/domain/services/AlarmManagerService";
import {
  ALARM_CFGLIST_INPUT,
  ALARM_CFGLIST_OUTPUT,
  ALARM_FILTER_INPUT,
  ALARM_FILTER_OUTPUT,
  ALARM_PAGING_CLIENT,
  ALARM_PAGING_END,
  ALARM_PAGING_INPUT,
  ALARM_PAGING_OUTPUT,
  ALARM_PAGING_START,
  ALARM_STATISTIC_CLIENT,
  ALARM_STATISTIC_INPUT,
  ALARM_STATISTIC_OUTPUT,
  ALARMCONFIG_UPDATE_CLIENT,
  ALARMCONFIG_UPDATE_INPUT,
  ALARMCONFIG_UPDATE_OUTPUT,
  ALARMLEVEL_UPDATE_CLIENT,
  ALARMLEVEL_UPDATE_INPUT,
  ALARMLEVEL_UPDATE_OUTPUT,
  TOTALCOUNTS_CLINET,
  TOTALCOUNTS_INPUT,
  TOTALCOUNTS_OUTPUT
} from "../../../mock/deviceState/AlarmManagerMock";
import {LINK_ADD_INPUT, LINK_ADD_OUTPUT} from "../../../mock/deviceState/LinkServiceMock";
import AlarmFilterConfigService from "../../../../src/domain/services/AlarmFilterConfigService";

describe('AlarmManager_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('AlarmManagerService (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) {
      //AlarmFilterConfigService.getAllAlarmFilterList();
      AlarmManagerService.getAlarmTypeList();
      AlarmManagerService.getAllAlarmCfgList();
      AlarmManagerService.getTotalCounts(TOTALCOUNTS_CLINET);
      AlarmManagerService.queryAlarmPaging(ALARM_PAGING_START,ALARM_PAGING_END,ALARM_PAGING_CLIENT);
      AlarmManagerService.queryAlarmStatistic(ALARM_STATISTIC_CLIENT);
      AlarmManagerService.updateAlarmLevel(ALARMLEVEL_UPDATE_CLIENT);
      AlarmManagerService.updateAlarmConfig(ALARMCONFIG_UPDATE_CLIENT);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        // console.log(moxios.requests);
        equal(moxios.requests.count(), 7);
        done()
      })
    });

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

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



    it('should getTotalCounts', function (done) {
      AlarmManagerService.getTotalCounts(TOTALCOUNTS_CLINET).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: TOTALCOUNTS_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, TOTALCOUNTS_OUTPUT);
          done()
        })
      })
    });

    it('should queryAlarmPaging', function (done) {
      AlarmManagerService.queryAlarmPaging(ALARM_PAGING_START,ALARM_PAGING_END,ALARM_PAGING_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARM_PAGING_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, ALARM_PAGING_OUTPUT);
          done()
        })
      })
    });

    it('should queryAlarmStatistic', function (done) {
      AlarmManagerService.queryAlarmStatistic(ALARM_STATISTIC_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARM_STATISTIC_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, ALARM_STATISTIC_OUTPUT);
          done()
        })
      })
    });

    it('should updateAlarmLevel', function (done) {
      AlarmManagerService.updateAlarmLevel(ALARMLEVEL_UPDATE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARMLEVEL_UPDATE_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, ALARMLEVEL_UPDATE_OUTPUT);
          done()
        })
      })
    });

    it('should updateAlarmConfig', function (done) {
      AlarmManagerService.updateAlarmConfig(ALARMCONFIG_UPDATE_CLIENT).then(onFulfilled);
      moxios.wait(function () {
        let request = moxios.requests.mostRecent();
        request.respondWith({
          status: 200,
          response: ALARMCONFIG_UPDATE_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, ALARMCONFIG_UPDATE_OUTPUT);
          done()
        })
      })
    });


  })
});
