<commit_before>import {
  discardPeriodicTasks,
  fakeAsync,
  TestBed,
  tick
} from '@angular/core/testing';

import {
  HttpModule,
  Response,
  ResponseOptions,
  XHRBackend
} from '@angular/http';

import {
  MockBackend,
  MockConnection
} from '@angular/http/testing';

import { createMock } from 'testing/mock';

import {
  Observable,
  Subscription
} from 'rxjs';

import {
  AuthenticationService,
  UserService
} from 'ngx-login-client';

import { WIT_API_URL } from 'ngx-fabric8-wit';

import { CpuStat } from '../models/cpu-stat';
import { Environment } from '../models/environment';
import { MemoryStat } from '../models/memory-stat';
import { ScaledMemoryStat } from '../models/scaled-memory-stat';
import {
  Application,
  DeploymentsService
} from './deployments.service';

describe('DeploymentsService', () => {

  let mockBackend: MockBackend;
  let svc: DeploymentsService;

  beforeEach(() => {
    const mockAuthService: jasmine.SpyObj<AuthenticationService> = createMock(AuthenticationService);
    mockAuthService.getToken.and.returnValue('mock-auth-token');

    TestBed.configureTestingModule({
      imports: [ HttpModule ],
      providers: [
        {
          provide: XHRBackend, useClass: MockBackend
        },
        {
          provide: AuthenticationService, useValue: mockAuthService
        },
        {
          provide: WIT_API_URL, useValue: 'http://example.com/'
        },
        DeploymentsService
      ]
    });
    svc = TestBed.get(DeploymentsService);
    mockBackend = TestBed.get(XHRBackend);
  });

  function doMockHttpTest<U>(response: any, expected: U, obs: Observable<U>, done?: DoneFn): void {
    const subscription: Subscription = mockBackend.connections.subscribe((connection: MockConnection) => {
      connection.mockRespond(new Response(
        new ResponseOptions({
          body: JSON.stringify(response),
          status: 200
        })
      ));
    });

    obs.subscribe((data: U) => {
      expect(data).toEqual(expected);
      subscription.unsubscribe();
      if (done) {
        done();
      }
    });
  }

  describe('#getApplications', () => {
    it('should publish faked application names', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: [
            { name: 'vertx-hello' }, { name: 'vertx-paint' }, { name: 'vertx-wiki' }
          ]
        }
      };
      doMockHttpTest(expectedResponse, expectedResponse.data.applications.map(app => app.name),
        svc.getApplications('foo-spaceId'), done);
    });

    it('should return empty array if no applications', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: []
        }
      };
      doMockHttpTest(expectedResponse, [],
        svc.getApplications('foo-spaceId'), done);
    });

    it('should return singleton array result', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: [
            { name: 'vertx-hello' }
          ]
        }
      };
      doMockHttpTest(expectedResponse, ['vertx-hello'],
        svc.getApplications('foo-spaceId'), done);
    });

    it('should return empty array for null applications response', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: null
        }
      };
      doMockHttpTest(expectedResponse, [], svc.getApplications('foo-spaceId'), done);
    });
  });

  describe('#getEnvironments', () => {
    it('should publish faked, filtered and sorted environments', (done: DoneFn) => {
      const expectedResponse = {
        data: [
          { name: 'stage' }, { name: 'run' }
        ]
      };
      doMockHttpTest(expectedResponse, expectedResponse.data, svc.getEnvironments('foo-spaceId'), done);
    });

    it('should return singleton array result', (done: DoneFn) => {
      const expectedResponse = {
        data: [
          { name: 'stage' }
        ]
      };
      doMockHttpTest(expectedResponse, expectedResponse.data, svc.getEnvironments('foo-spaceId'), done);
    });
<commit_msg>fix(deployments): fix deployments service environments test<commit_after>import {
  discardPeriodicTasks,
  fakeAsync,
  TestBed,
  tick
} from '@angular/core/testing';

import {
  HttpModule,
  Response,
  ResponseOptions,
  XHRBackend
} from '@angular/http';

import {
  MockBackend,
  MockConnection
} from '@angular/http/testing';

import { createMock } from 'testing/mock';

import {
  Observable,
  Subscription
} from 'rxjs';

import {
  AuthenticationService,
  UserService
} from 'ngx-login-client';

import { WIT_API_URL } from 'ngx-fabric8-wit';

import { CpuStat } from '../models/cpu-stat';
import { Environment } from '../models/environment';
import { MemoryStat } from '../models/memory-stat';
import { ScaledMemoryStat } from '../models/scaled-memory-stat';
import {
  Application,
  DeploymentsService
} from './deployments.service';

describe('DeploymentsService', () => {

  let mockBackend: MockBackend;
  let svc: DeploymentsService;

  beforeEach(() => {
    const mockAuthService: jasmine.SpyObj<AuthenticationService> = createMock(AuthenticationService);
    mockAuthService.getToken.and.returnValue('mock-auth-token');

    TestBed.configureTestingModule({
      imports: [ HttpModule ],
      providers: [
        {
          provide: XHRBackend, useClass: MockBackend
        },
        {
          provide: AuthenticationService, useValue: mockAuthService
        },
        {
          provide: WIT_API_URL, useValue: 'http://example.com/'
        },
        DeploymentsService
      ]
    });
    svc = TestBed.get(DeploymentsService);
    mockBackend = TestBed.get(XHRBackend);
  });

  function doMockHttpTest<U>(response: any, expected: U, obs: Observable<U>, done?: DoneFn): void {
    const subscription: Subscription = mockBackend.connections.subscribe((connection: MockConnection) => {
      connection.mockRespond(new Response(
        new ResponseOptions({
          body: JSON.stringify(response),
          status: 200
        })
      ));
    });

    obs.subscribe((data: U) => {
      expect(data).toEqual(expected);
      subscription.unsubscribe();
      if (done) {
        done();
      }
    });
  }

  describe('#getApplications', () => {
    it('should publish faked application names', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: [
            { name: 'vertx-hello' }, { name: 'vertx-paint' }, { name: 'vertx-wiki' }
          ]
        }
      };
      doMockHttpTest(expectedResponse, expectedResponse.data.applications.map(app => app.name),
        svc.getApplications('foo-spaceId'), done);
    });

    it('should return empty array if no applications', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: []
        }
      };
      doMockHttpTest(expectedResponse, [],
        svc.getApplications('foo-spaceId'), done);
    });

    it('should return singleton array result', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: [
            { name: 'vertx-hello' }
          ]
        }
      };
      doMockHttpTest(expectedResponse, ['vertx-hello'],
        svc.getApplications('foo-spaceId'), done);
    });

    it('should return empty array for null applications response', (done: DoneFn) => {
      const expectedResponse = {
        data: {
          applications: null
        }
      };
      doMockHttpTest(expectedResponse, [], svc.getApplications('foo-spaceId'), done);
    });
  });

  describe('#getEnvironments', () => {
    it('should publish faked, filtered and sorted environments', (done: DoneFn) => {
      const httpResponse = {
        data: [
          { name: 'run' }, { name: 'test' }, { name: 'stage'}
        ]
      };
      const expectedResponse = [{ name: 'stage' }, { name: 'run' }];
      doMockHttpTest(httpResponse, expectedResponse, svc.getEnvironments('foo-spaceId'), done);
    });

    it('should return singleton array result', (done: DoneFn) => {
      const expectedResponse = {
        data: [
          { name: 'stage' }
        ]
      };
      doMockHttpTest(expectedResponse, expectedResponse.data, svc.getEnvironments('foo-spaceId'), done);
    });

{'fraction_non_alphanumeric': 0.13818578176093013, 'fraction_numerical': 0.0009688748940293085, 'mean_word_length': 2.658839167035888, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5629811', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2385, 'n_words': 479}