import {SettingsService} from './settings.service';
import {QueueSetting, Setting, WorkflowSetting} from "../model/setting";
import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
import {TestBed} from "@angular/core/testing";
import {ConfigurationService} from "../env/configuration.service";

export class FakeConfigurationService {
  get config() {
    return {
      dryrun: "false",
      rootDataDirectory: "/lustre/aoc/cluster/pipeline/vlass_test/spool",
      url: "http://localhost:4200/VlassMngr",
      weblogbaseurl: "http://webtest.aoc.nrao.edu/",
      projectcode: "VLASS1.1",
      wsurl: "ws://localhost:8081/VlassMngr/changes",
      currentepoch: "1"
    };
  }
}

describe('SettingsService', () => {

  let httpMock: HttpTestingController;
  let settingsService: SettingsService;
  let fakeConfigService: FakeConfigurationService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [SettingsService, {provide: ConfigurationService, useClass: FakeConfigurationService}]
    });

    settingsService = TestBed.get(SettingsService);
    httpMock = TestBed.get(HttpTestingController);
    fakeConfigService = TestBed.get(ConfigurationService);
  });

  it('should get settings', () => {
    let expectedResponse = {allowJobDeletion: true, allowProductDeletion: false, id: "X"} as Setting;

    settingsService.getSettings().subscribe(response => {
      expect(response).toEqual(expectedResponse, 'get settings');
    });

    const req = httpMock.expectOne(fakeConfigService.config.url + '/services/settings');
    expect(req.request.method).toBe("GET");
    req.flush(expectedResponse);

    httpMock.verify();
  });

  it('should set settings', () => {
    let expectedResponse = {allowJobDeletion: true, allowProductDeletion: false, id: "X"} as Setting;

    settingsService.setSettings(expectedResponse).subscribe(response => {
      expect(response).toEqual(expectedResponse, 'set settings');
    });

    const req = httpMock.expectOne(fakeConfigService.config.url);
    expect(req.request.method).toBe("POST");
    req.flush(expectedResponse);

    httpMock.verify();
  });

  it('should get queue settings', () => {
    let expectedResponse = [
      {name: "calibration", autoCreateJob: true, autoSubmitJob: true} as QueueSetting,
      {name: "compression", autoCreateJob: false, autoSubmitJob: false} as QueueSetting
    ];

    settingsService.getQueueSettings().subscribe(response => {
      expect(response).toEqual(expectedResponse, 'get queue settings');
    });

    const req = httpMock.expectOne(fakeConfigService.config.url + '/services/job/queueSettings');
    expect(req.request.method).toBe("GET");
    req.flush(expectedResponse);

    httpMock.verify();
  });

  it('should get workflow settings', () => {
    let expectedResponse = [{
      archivalWorkflow: null,
      enqueue: false,
      fileTemplates: [],
      host: null,
      id: 1,
      json: null,
      name: "compression",
      port: null,
      projectCodes: [],
      taskTemplates: []
    } as WorkflowSetting, {
      archivalWorkflow: null,
      enqueue: false,
      fileTemplates: [],
      host: null,
      id: 2,
      json: null,
      name: "calibration",
      port: null,
      projectCodes: [],
      taskTemplates: []
    } as WorkflowSetting];

    settingsService.getWorkflowSettings().subscribe(response => {
      expect(response).toEqual(expectedResponse, 'get workflow settings');
    });

    const req = httpMock.expectOne(fakeConfigService.config.url + '/services/job/queueWorkflows');
    expect(req.request.method).toBe("GET");
    req.flush(expectedResponse);

    httpMock.verify();
  });

  it('should delete file', () => {

    settingsService.deleteFile('calibration', 'calname').subscribe(response => {
      expect(response).toEqual(200, 'delete file');
    });
    const req = httpMock.expectOne(fakeConfigService.config.url + "/services/job/queues/calibration/workflow/files/calname");
    expect(req.request.method).toBe("DELETE");

    httpMock.verify();
  });
});