<commit_before>import { Observable } from 'rxjs';
import {
  cassandraDriver,
  cassandraFailingToConnectDriver,
} from './mocks/cassandra-driver';
jest.doMock('cassandra-driver', cassandraDriver);
import { Application } from 'express';

import chai = require('chai');
import chaiHttp = require('chai-http');
chai.use(chaiHttp);

const expect = chai.expect;

const kafkaMock = {
  createTopic: jest.fn(() => Promise.resolve()),
  sendMessage: jest.fn(() => Promise.resolve()),
  sendPayloads: jest.fn(() => Promise.resolve()),
  rawMessages: jest.fn(() => Promise.resolve()),
  isConnected: jest.fn(() => Promise.resolve(true)),
};

const kafkaFailingToConnectMock = {
  createTopic: jest.fn(() => Promise.resolve()),
  sendMessage: jest.fn(() => Promise.resolve()),
  sendPayloads: jest.fn(() => Promise.resolve()),
  rawMessages: jest.fn(() => Promise.resolve()),
  isConnected: () => {
    throw new Error('No Kafka, sorry');
  },
};

jest.doMock('dav-js', () => ({ KafkaNode: jest.fn(() => kafkaMock) }));

describe('App', () => {
  beforeEach(() => {
    jest.resetAllMocks();
    jest.resetModules();
  });

  describe('baseRoute', () => {
    let app: Application;

    beforeAll(async () => {
      app = (await import('./App')).default;
    });

    it('should return HTTP 200', async () => {
      const res = await chai.request(app).get('/');
      expect(res.status).to.eql(200);
    });

    it('should be json', async () => {
      const res = await chai.request(app).get('/');
      expect(res.type).to.eql('application/json');
    });

    it('should have a message prop', async () => {
      const res = await chai.request(app).get('/');
      expect(res.body.message).to.eql('DAV Network Node');
    });
  });

  describe('status (ok)', () => {
    let app: Application;
    beforeAll(async () => {
      app = (await import('./App')).default;
    });

    // TODO: Expected values are usually per test - not suite.
    const expectedResult = {
      app: { connected: true },
      kafka: { connected: true },
      cassandra: {
        connected: true,
        hosts: [{ address: 'localhost', connections: 0, queries: 1 }],
      },
    };

    it('should return status connected', async () => {
      const res = await chai.request(app).get('/health');
      expect(res.body.message).to.eql(expectedResult);
    });
  });
  describe('status (failure)', () => {
    let app: Application;
    beforeAll(async () => {
      jest.doMock('cassandra-driver', cassandraFailingToConnectDriver);
      app = (await import('./App')).default;
    });

    const expectedResult = {
      app: { connected: true },
      kafka: { connected: true },
      cassandra: { connected: false },
    };

    it('should return status HTTP 503 when there is no Cassandra', async () => {
      const res = await chai.request(app).get('/health');
      expect(res.status).to.eql(503);
    });

    it('should return error when there is no Cassandra', async () => {
      const res = await chai.request(app).get('/health');
      expect(res.body.message).to.eql(expectedResult);
    });

    afterAll(async () => {
      jest.doMock('cassandra-driver', cassandraDriver);
    });
  });

  describe('provider', () => {
    let app: Application;

    beforeAll(async () => {
      app = (await import('./App')).default;
    });

    // TODO: Should this maybe be in a test - not suite.
    const requestData = {
      davId: 'david1',
      area: {
        min: { latitude: 43.331, longitude: 82.762 },
        max: { latitude: 43.396312072116764, longitude: 83.08981250773137 },
      },
      dimensions: {
        length: 1,
        width: 1,
        height: 1,
      },
      protocol: 'drone_delivery',
    };

    it('should save provider', async () => {
      const res = await chai
        .request(app)
        .post('/needsForType/topic1')
        .send(requestData);<commit_msg>Mock Kafka with a failed to connect instance for the failed status test suite<commit_after>import { Observable } from 'rxjs';
import {
  cassandraDriver,
  cassandraFailingToConnectDriver,
} from './mocks/cassandra-driver';
jest.doMock('cassandra-driver', cassandraDriver);
import { Application } from 'express';

import chai = require('chai');
import chaiHttp = require('chai-http');
chai.use(chaiHttp);

const expect = chai.expect;

const kafkaMock = {
  createTopic: jest.fn(() => Promise.resolve()),
  sendMessage: jest.fn(() => Promise.resolve()),
  sendPayloads: jest.fn(() => Promise.resolve()),
  rawMessages: jest.fn(() => Promise.resolve()),
  isConnected: jest.fn(() => Promise.resolve(true)),
};

const kafkaFailingToConnectMock = {
  createTopic: jest.fn(() => Promise.resolve()),
  sendMessage: jest.fn(() => Promise.resolve()),
  sendPayloads: jest.fn(() => Promise.resolve()),
  rawMessages: jest.fn(() => Promise.resolve()),
  isConnected: () => {
    throw new Error('No Kafka, sorry');
  },
};

jest.doMock('dav-js', () => ({ KafkaNode: jest.fn(() => kafkaMock) }));

describe('App', () => {
  beforeEach(() => {
    jest.resetAllMocks();
    jest.resetModules();
  });

  describe('baseRoute', () => {
    let app: Application;

    beforeAll(async () => {
      app = (await import('./App')).default;
    });

    it('should return HTTP 200', async () => {
      const res = await chai.request(app).get('/');
      expect(res.status).to.eql(200);
    });

    it('should be json', async () => {
      const res = await chai.request(app).get('/');
      expect(res.type).to.eql('application/json');
    });

    it('should have a message prop', async () => {
      const res = await chai.request(app).get('/');
      expect(res.body.message).to.eql('DAV Network Node');
    });
  });

  describe('status (ok)', () => {
    let app: Application;
    beforeAll(async () => {
      app = (await import('./App')).default;
    });

    // TODO: Expected values are usually per test - not suite.
    const expectedResult = {
      app: { connected: true },
      kafka: { connected: true },
      cassandra: {
        connected: true,
        hosts: [{ address: 'localhost', connections: 0, queries: 1 }],
      },
    };

    it('should return status connected', async () => {
      const res = await chai.request(app).get('/health');
      expect(res.body.message).to.eql(expectedResult);
    });
  });
  describe('status (failure)', () => {
    let app: Application;
    beforeAll(async () => {
      jest.doMock('cassandra-driver', cassandraFailingToConnectDriver);
      jest.doMock('dav-js', () => ({ KafkaNode: jest.fn(() => kafkaFailingToConnectMock) }));
      app = (await import('./App')).default;
    });

    const expectedResult = {
      app: { connected: true },
      kafka: { connected: true },
      cassandra: { connected: false },
    };

    it('should return status HTTP 503 when there is no Cassandra', async () => {
      const res = await chai.request(app).get('/health');
      expect(res.status).to.eql(503);
    });

    it('should return error when there is no Cassandra', async () => {
      const res = await chai.request(app).get('/health');
      expect(res.body.message).to.eql(expectedResult);
    });

    afterAll(async () => {
      jest.doMock('cassandra-driver', cassandraDriver);
      jest.doMock('dav-js', () => ({ KafkaNode: jest.fn(() => kafkaMock) }));
    });
  });

  describe('provider', () => {
    let app: Application;

    beforeAll(async () => {
      app = (await import('./App')).default;
    });

    // TODO: Should this maybe be in a test - not suite.
    const requestData = {
      davId: 'david1',
      area: {
        min: { latitude: 43.331, longitude: 82.762 },
        max: { latitude: 43.396312072116764, longitude: 83.08981250773137 },
      },
      dimensions: {
        length: 1,
        width: 1,
        height: 1,
      },
      protocol: 'drone_delivery',
    };

    it('should save provider', async () => {
      const res = await chai
        .request(app)
        .post('/needsForType/topic1')
        .send(requestData);
{'fraction_non_alphanumeric': 0.18554638659664915, 'fraction_numerical': 0.015503875968992248, 'mean_word_length': 2.883009708737864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 67, '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': '298045', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2476, 'n_words': 548}