From b05d0461576d2efbf92aeb7f2ca6518d932fe291 Mon Sep 17 00:00:00 2001 From: burgerni10 Date: Wed, 22 May 2024 12:38:13 +0200 Subject: [PATCH] feat(external-source): deprecate and remove external sources --- .../entity-migrations/v3.0-initial-setup.ts | 6 +- .../v3.4.0-deprecate-north-oibus.ts | 9 ++ backend/src/north/north-connector.spec.ts | 18 --- backend/src/north/north-connector.ts | 12 +- .../external-source.repository.spec.ts | 100 ------------ .../repository/external-source.repository.ts | 64 -------- .../subscription.repository.spec.ts | 48 +----- .../src/repository/subscription.repository.ts | 46 +----- backend/src/service/reload.service.spec.ts | 36 ----- backend/src/service/reload.service.ts | 18 --- .../src/service/repository.service.spec.ts | 4 - backend/src/service/repository.service.ts | 7 - .../__mocks__/repository-service.mock.ts | 7 +- .../controllers/content.controller.spec.ts | 40 +---- .../controllers/content.controller.ts | 10 +- .../external-source.controller.spec.ts | 134 ---------------- .../controllers/external-source.controller.ts | 51 ------ .../north-connector.controller.spec.ts | 14 +- .../controllers/north-connector.controller.ts | 24 --- .../subscription.controller.spec.ts | 150 ------------------ .../controllers/subscription.controller.ts | 46 +----- backend/src/web-server/routes/index.ts | 18 --- shared/model/external-sources.model.ts | 18 --- shared/model/subscription.model.ts | 3 - 24 files changed, 25 insertions(+), 858 deletions(-) delete mode 100644 backend/src/repository/external-source.repository.spec.ts delete mode 100644 backend/src/repository/external-source.repository.ts delete mode 100644 backend/src/web-server/controllers/external-source.controller.spec.ts delete mode 100644 backend/src/web-server/controllers/external-source.controller.ts delete mode 100644 shared/model/external-sources.model.ts diff --git a/backend/src/db/entity-migrations/v3.0-initial-setup.ts b/backend/src/db/entity-migrations/v3.0-initial-setup.ts index 340ade663d..9771e2adcf 100644 --- a/backend/src/db/entity-migrations/v3.0-initial-setup.ts +++ b/backend/src/db/entity-migrations/v3.0-initial-setup.ts @@ -2,7 +2,6 @@ import { Knex } from 'knex'; import { LOG_LEVELS } from '../../../../shared/model/engine.model'; import { ENGINES_TABLE } from '../../repository/engine.repository'; import { USERS_TABLE } from '../../repository/user.repository'; -import { EXTERNAL_SOURCES_TABLE } from '../../repository/external-source.repository'; import { IP_FILTERS_TABLE } from '../../repository/ip-filter.repository'; import { SCAN_MODES_TABLE } from '../../repository/scan-mode.repository'; import { HISTORY_QUERIES_TABLE } from '../../repository/history-query.repository'; @@ -11,7 +10,10 @@ import { SOUTH_CONNECTORS_TABLE } from '../../repository/south-connector.reposit import { SOUTH_ITEMS_TABLE } from '../../repository/south-item.repository'; import { NORTH_CONNECTORS_TABLE } from '../../repository/north-connector.repository'; import CreateTableBuilder = Knex.CreateTableBuilder; -import { EXTERNAL_SUBSCRIPTION_TABLE, SUBSCRIPTION_TABLE } from '../../repository/subscription.repository'; +import { SUBSCRIPTION_TABLE } from '../../repository/subscription.repository'; + +const EXTERNAL_SOURCES_TABLE = 'external_sources'; +const EXTERNAL_SUBSCRIPTION_TABLE = 'external_subscription'; export async function up(knex: Knex): Promise { await createEnginesTable(knex); diff --git a/backend/src/db/entity-migrations/v3.4.0-deprecate-north-oibus.ts b/backend/src/db/entity-migrations/v3.4.0-deprecate-north-oibus.ts index 90fd1ac9be..25500fb28a 100644 --- a/backend/src/db/entity-migrations/v3.4.0-deprecate-north-oibus.ts +++ b/backend/src/db/entity-migrations/v3.4.0-deprecate-north-oibus.ts @@ -6,9 +6,13 @@ import { filesExists } from '../../service/utils'; import fs from 'node:fs/promises'; import { HISTORY_ITEMS_TABLE } from '../../repository/history-query-item.repository'; +const EXTERNAL_SOURCES_TABLE = 'external_sources'; +const EXTERNAL_SUBSCRIPTION_TABLE = 'external_subscription'; + export async function up(knex: Knex): Promise { await removeNorthOIBusConnectors(knex); await removeNorthOIBusHistoryQueries(knex); + await removeExternalSubscriptions(knex); } async function removeNorthOIBusConnectors(knex: Knex): Promise { @@ -39,4 +43,9 @@ async function removeNorthOIBusHistoryQueries(knex: Knex): Promise { await knex(HISTORY_QUERIES_TABLE).delete().where('north_type', 'oibus'); } +async function removeExternalSubscriptions(knex: Knex): Promise { + await knex.schema.dropTableIfExists(EXTERNAL_SUBSCRIPTION_TABLE); + await knex.schema.dropTableIfExists(EXTERNAL_SOURCES_TABLE); +} + export async function down(): Promise {} diff --git a/backend/src/north/north-connector.spec.ts b/backend/src/north/north-connector.spec.ts index d7854d6928..4cf6e1e14a 100644 --- a/backend/src/north/north-connector.spec.ts +++ b/backend/src/north/north-connector.spec.ts @@ -353,36 +353,18 @@ describe('NorthConnector enabled', () => { expect(north.isSubscribed('southId')).toBeTruthy(); }); - it('should check if North is subscribed to all external sources', async () => { - (repositoryService.subscriptionRepository.getExternalNorthSubscriptions as jest.Mock).mockReturnValue([]); - await north.start(); - expect(north.isSubscribedToExternalSource('externalSourceId')).toBeTruthy(); - }); - it('should check if North is subscribed to South', async () => { (repositoryService.subscriptionRepository.getNorthSubscriptions as jest.Mock).mockReturnValue(['southId']); await north.start(); expect(north.isSubscribed('southId')).toBeTruthy(); }); - it('should check if North is subscribed to external source', async () => { - (repositoryService.subscriptionRepository.getExternalNorthSubscriptions as jest.Mock).mockReturnValue(['externalSourceId']); - await north.start(); - expect(north.isSubscribedToExternalSource('externalSourceId')).toBeTruthy(); - }); - it('should check if North is not subscribed to South', async () => { (repositoryService.subscriptionRepository.getNorthSubscriptions as jest.Mock).mockReturnValue(['southId1']); await north.start(); expect(north.isSubscribed('southId2')).toBeFalsy(); }); - it('should check if North is not subscribed to external source', async () => { - (repositoryService.subscriptionRepository.getExternalNorthSubscriptions as jest.Mock).mockReturnValue(['externalSourceId1']); - await north.start(); - expect(north.isSubscribedToExternalSource('externalSourceId2')).toBeFalsy(); - }); - it('should get error files', async () => { const result = await north.getErrorFiles('2022-11-11T11:11:11.111Z', '2022-11-12T11:11:11.111Z', 'file'); expect(result).toEqual([ diff --git a/backend/src/north/north-connector.ts b/backend/src/north/north-connector.ts index b2bda72551..fcfa3c246c 100644 --- a/backend/src/north/north-connector.ts +++ b/backend/src/north/north-connector.ts @@ -11,7 +11,7 @@ import { EventEmitter } from 'node:events'; import { ScanModeDTO } from '../../../shared/model/scan-mode.model'; import DeferredPromise from '../service/deferred-promise'; import { OIBusContent, OIBusError, OIBusRawContent, OIBusTimeValue, OIBusTimeValueContent } from '../../../shared/model/engine.model'; -import { ExternalSubscriptionDTO, SubscriptionDTO } from '../../../shared/model/subscription.model'; +import { SubscriptionDTO } from '../../../shared/model/subscription.model'; import { DateTime } from 'luxon'; import { PassThrough } from 'node:stream'; import { ReadStream } from 'node:fs'; @@ -45,7 +45,6 @@ export default class NorthConnector { private fileCacheService: FileCacheService; protected metricsService: NorthConnectorMetricsService | null = null; private subscribedTo: Array = []; - private subscribedToExternalSources: Array = []; private cacheSize = 0; private fileBeingSent: string | null = null; @@ -142,7 +141,6 @@ export default class NorthConnector { cacheSize: this.cacheSize }); this.subscribedTo = this.repositoryService.subscriptionRepository.getNorthSubscriptions(this.connector.id); - this.subscribedToExternalSources = this.repositoryService.subscriptionRepository.getExternalNorthSubscriptions(this.connector.id); } await this.valueCacheService.start(); await this.fileCacheService.start(); @@ -394,14 +392,6 @@ export default class NorthConnector { return this.subscribedTo.length === 0 || this.subscribedTo.includes(southId); } - /** - * Check whether the North is subscribed to an external source. - * If subscribedToExternalSources is not defined or an empty array, the subscription is true. - */ - isSubscribedToExternalSource(externalSourceId: string): boolean { - return this.subscribedToExternalSources.length === 0 || this.subscribedToExternalSources.includes(externalSourceId); - } - /** * Check appropriate caches emptiness */ diff --git a/backend/src/repository/external-source.repository.spec.ts b/backend/src/repository/external-source.repository.spec.ts deleted file mode 100644 index aa01335d42..0000000000 --- a/backend/src/repository/external-source.repository.spec.ts +++ /dev/null @@ -1,100 +0,0 @@ -import SqliteDatabaseMock, { all, get, run } from '../tests/__mocks__/database.mock'; -import { generateRandomId } from '../service/utils'; -import ExternalSourceRepository from './external-source.repository'; -import { ExternalSourceCommandDTO, ExternalSourceDTO } from '../../../shared/model/external-sources.model'; -import { Database } from 'better-sqlite3'; - -jest.mock('../service/utils', () => ({ - generateRandomId: jest.fn(() => '123456') -})); - -let database: Database; -let repository: ExternalSourceRepository; -describe('External source repository', () => { - beforeEach(() => { - jest.clearAllMocks(); - database = new SqliteDatabaseMock(); - database.prepare = jest.fn().mockReturnValue({ - run, - get, - all - }); - repository = new ExternalSourceRepository(database); - }); - - it('should properly get all external sources', () => { - const expectedValue: Array = [ - { - id: 'id1', - reference: 'ref1', - description: 'my first external source' - }, - { - id: 'id2', - reference: 'ref2', - description: 'my second external source' - } - ]; - all.mockReturnValueOnce(expectedValue); - const externalSources = repository.getExternalSources(); - expect(database.prepare).toHaveBeenCalledWith('SELECT id, reference, description FROM external_sources;'); - expect(externalSources).toEqual(expectedValue); - }); - - it('should properly get an external source', () => { - const expectedValue: ExternalSourceDTO = { - id: 'id1', - reference: 'ref1', - description: 'my first external source' - }; - get.mockReturnValueOnce(expectedValue); - const externalSource = repository.getExternalSource('id1'); - expect(database.prepare).toHaveBeenCalledWith('SELECT id, reference, description FROM external_sources WHERE id = ?;'); - expect(get).toHaveBeenCalledWith('id1'); - expect(externalSource).toEqual(expectedValue); - }); - - it('should properly get an external source by reference', () => { - const expectedValue: ExternalSourceDTO = { - id: 'id1', - reference: 'ref1', - description: 'my first external source' - }; - get.mockReturnValueOnce(expectedValue); - const externalSource = repository.findExternalSourceByReference('ref1'); - expect(database.prepare).toHaveBeenCalledWith('SELECT id, reference, description FROM external_sources WHERE reference = ?;'); - expect(get).toHaveBeenCalledWith('ref1'); - expect(externalSource).toEqual(expectedValue); - }); - - it('should create an external source', () => { - run.mockReturnValueOnce({ lastInsertRowid: 1 }); - - const command: ExternalSourceCommandDTO = { - reference: 'ref1', - description: 'my first external source' - }; - repository.createExternalSource(command); - expect(generateRandomId).toHaveBeenCalledWith(6); - expect(database.prepare).toHaveBeenCalledWith('INSERT INTO external_sources (id, reference, description) VALUES (?, ?, ?);'); - expect(run).toHaveBeenCalledWith('123456', command.reference, command.description); - - expect(database.prepare).toHaveBeenCalledWith('SELECT id, reference, description FROM external_sources WHERE ROWID = ?;'); - }); - - it('should update an external source', () => { - const command: ExternalSourceCommandDTO = { - reference: 'ref1', - description: 'my first external source' - }; - repository.updateExternalSource('id1', command); - expect(database.prepare).toHaveBeenCalledWith('UPDATE external_sources SET reference = ?, description = ? WHERE id = ?;'); - expect(run).toHaveBeenCalledWith(command.reference, command.description, 'id1'); - }); - - it('should delete an external source', () => { - repository.deleteExternalSource('id1'); - expect(database.prepare).toHaveBeenCalledWith('DELETE FROM external_sources WHERE id = ?;'); - expect(run).toHaveBeenCalledWith('id1'); - }); -}); diff --git a/backend/src/repository/external-source.repository.ts b/backend/src/repository/external-source.repository.ts deleted file mode 100644 index b403d9cf36..0000000000 --- a/backend/src/repository/external-source.repository.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { generateRandomId } from '../service/utils'; -import { ExternalSourceCommandDTO, ExternalSourceDTO } from '../../../shared/model/external-sources.model'; -import { Database } from 'better-sqlite3'; - -export const EXTERNAL_SOURCES_TABLE = 'external_sources'; - -/** - * Repository used for external sources (remote data source that send values directly to the API) - */ -export default class ExternalSourceRepository { - constructor(private readonly database: Database) {} - - /** - * Retrieve all external sources - */ - getExternalSources(): Array { - const query = `SELECT id, reference, description FROM ${EXTERNAL_SOURCES_TABLE};`; - return this.database.prepare(query).all() as Array; - } - - /** - * Retrieve an external source by its ID - */ - getExternalSource(id: string): ExternalSourceDTO | null { - const query = `SELECT id, reference, description FROM ${EXTERNAL_SOURCES_TABLE} WHERE id = ?;`; - return this.database.prepare(query).get(id) as ExternalSourceDTO | null; - } - - /** - * Retrieve an external source by its reference - */ - findExternalSourceByReference(reference: string): ExternalSourceDTO | null { - const query = `SELECT id, reference, description FROM ${EXTERNAL_SOURCES_TABLE} WHERE reference = ?;`; - return this.database.prepare(query).get(reference) as ExternalSourceDTO | null; - } - - /** - * Create an external source with a random generated ID - */ - createExternalSource(command: ExternalSourceCommandDTO): ExternalSourceDTO { - const id = generateRandomId(6); - const insertQuery = `INSERT INTO ${EXTERNAL_SOURCES_TABLE} (id, reference, description) ` + `VALUES (?, ?, ?);`; - const result = this.database.prepare(insertQuery).run(id, command.reference, command.description); - - const query = `SELECT id, reference, description FROM ${EXTERNAL_SOURCES_TABLE} WHERE ROWID = ?;`; - return this.database.prepare(query).get(result.lastInsertRowid) as ExternalSourceDTO; - } - - /** - * Update an external source by its ID - */ - updateExternalSource(id: string, command: ExternalSourceCommandDTO): void { - const query = `UPDATE ${EXTERNAL_SOURCES_TABLE} SET reference = ?, description = ? WHERE id = ?;`; - this.database.prepare(query).run(command.reference, command.description, id); - } - - /** - * Delete an external source by its ID - */ - deleteExternalSource(id: string): void { - const query = `DELETE FROM ${EXTERNAL_SOURCES_TABLE} WHERE id = ?;`; - this.database.prepare(query).run(id); - } -} diff --git a/backend/src/repository/subscription.repository.spec.ts b/backend/src/repository/subscription.repository.spec.ts index d1de4e15ad..cbadc4a3c9 100644 --- a/backend/src/repository/subscription.repository.spec.ts +++ b/backend/src/repository/subscription.repository.spec.ts @@ -1,7 +1,7 @@ import SubscriptionRepository from './subscription.repository'; -import SqliteDatabaseMock, { run, all, get } from '../tests/__mocks__/database.mock'; +import SqliteDatabaseMock, { all, get, run } from '../tests/__mocks__/database.mock'; import { Database } from 'better-sqlite3'; -import { ExternalSubscriptionDTO, SubscriptionDTO } from '../../../shared/model/subscription.model'; +import { SubscriptionDTO } from '../../../shared/model/subscription.model'; jest.mock('../tests/__mocks__/database.mock'); @@ -29,16 +29,6 @@ describe('Subscription repository', () => { expect(subscriptions).toEqual(expectedValue); }); - it('should properly get all external subscriptions', () => { - const expectedValue: Array = ['external1', 'external2']; - all.mockReturnValueOnce(expectedValue.map(sourceId => ({ externalSourceId: sourceId }))); - const subscriptions = repository.getExternalNorthSubscriptions('north1'); - expect(database.prepare).toHaveBeenCalledWith( - 'SELECT external_source_id AS externalSourceId FROM external_subscription WHERE north_connector_id = ?;' - ); - expect(subscriptions).toEqual(expectedValue); - }); - it('should properly get all subscribed North connectors', () => { const expectedValue = ['north1', 'north2']; all.mockReturnValueOnce(expectedValue.map(northId => ({ northConnectorId: northId }))); @@ -59,16 +49,6 @@ describe('Subscription repository', () => { expect(get).toHaveBeenCalledWith('north1', 'south1'); }); - it('should check an external subscription', () => { - get.mockReturnValueOnce({ externalSourceId: 'external1' }); - - repository.checkExternalNorthSubscription('north1', 'external1'); - expect(database.prepare).toHaveBeenCalledWith( - 'SELECT external_source_id AS externalSourceId FROM external_subscription WHERE north_connector_id = ? AND external_source_id = ?;' - ); - expect(get).toHaveBeenCalledWith('north1', 'external1'); - }); - it('should create a subscription', () => { run.mockReturnValueOnce({ lastInsertRowid: 1 }); @@ -77,39 +57,15 @@ describe('Subscription repository', () => { expect(run).toHaveBeenCalledWith('north1', 'south1'); }); - it('should create an external subscription', () => { - run.mockReturnValueOnce({ lastInsertRowid: 1 }); - - repository.createExternalNorthSubscription('north1', 'external1'); - expect(database.prepare).toHaveBeenCalledWith( - 'INSERT INTO external_subscription (north_connector_id, external_source_id) VALUES (?, ?);' - ); - expect(run).toHaveBeenCalledWith('north1', 'external1'); - }); - it('should delete a subscription', () => { repository.deleteNorthSubscription('north1', 'south1'); expect(database.prepare).toHaveBeenCalledWith('DELETE FROM subscription WHERE north_connector_id = ? AND south_connector_id = ?;'); expect(run).toHaveBeenCalledWith('north1', 'south1'); }); - it('should delete an external subscription', () => { - repository.deleteExternalNorthSubscription('north1', 'external1'); - expect(database.prepare).toHaveBeenCalledWith( - 'DELETE FROM external_subscription WHERE north_connector_id = ? AND external_source_id = ?;' - ); - expect(run).toHaveBeenCalledWith('north1', 'external1'); - }); - it('should delete all North subscriptions', () => { repository.deleteNorthSubscriptions('north1'); expect(database.prepare).toHaveBeenCalledWith('DELETE FROM subscription WHERE north_connector_id = ?;'); expect(run).toHaveBeenCalledWith('north1'); }); - - it('should delete all external North subscriptions', () => { - repository.deleteExternalNorthSubscriptions('north1'); - expect(database.prepare).toHaveBeenCalledWith('DELETE FROM external_subscription WHERE north_connector_id = ?;'); - expect(run).toHaveBeenCalledWith('north1'); - }); }); diff --git a/backend/src/repository/subscription.repository.ts b/backend/src/repository/subscription.repository.ts index 4d118d14ef..797f6f37f9 100644 --- a/backend/src/repository/subscription.repository.ts +++ b/backend/src/repository/subscription.repository.ts @@ -1,8 +1,7 @@ import { Database } from 'better-sqlite3'; -import { ExternalSubscriptionDTO, SubscriptionDTO } from '../../../shared/model/subscription.model'; +import { SubscriptionDTO } from '../../../shared/model/subscription.model'; export const SUBSCRIPTION_TABLE = 'subscription'; -export const EXTERNAL_SUBSCRIPTION_TABLE = 'external_subscription'; /** * Repository used for subscriptions @@ -21,17 +20,6 @@ export default class SubscriptionRepository { .map((row: any) => row.southConnectorId); } - /** - * Retrieve all external subscriptions for a given North connector - */ - getExternalNorthSubscriptions(northId: string): Array { - const query = `SELECT external_source_id AS externalSourceId FROM ${EXTERNAL_SUBSCRIPTION_TABLE} WHERE north_connector_id = ?;`; - return this.database - .prepare(query) - .all(northId) - .map((row: any) => row.externalSourceId); - } - /** * Retrieve all subscribed North connectors for a given South connector */ @@ -51,14 +39,6 @@ export default class SubscriptionRepository { return !!this.database.prepare(query).get(northId, southId); } - /** - * Check whether an external subscription exists for a given North connector - */ - checkExternalNorthSubscription(northId: string, externalSourceId: string): boolean { - const query = `SELECT external_source_id AS externalSourceId FROM ${EXTERNAL_SUBSCRIPTION_TABLE} WHERE north_connector_id = ? AND external_source_id = ?;`; - return !!this.database.prepare(query).get(northId, externalSourceId); - } - /** * Create a subscription for a given North connector */ @@ -67,14 +47,6 @@ export default class SubscriptionRepository { this.database.prepare(query).run(northId, southId); } - /** - * Create an external subscription for a given North connector - */ - createExternalNorthSubscription(northId: string, externalSourceId: string): void { - const query = `INSERT INTO ${EXTERNAL_SUBSCRIPTION_TABLE} (north_connector_id, external_source_id) ` + 'VALUES (?, ?);'; - this.database.prepare(query).run(northId, externalSourceId); - } - /** * Delete a subscription for a given North connector */ @@ -83,14 +55,6 @@ export default class SubscriptionRepository { this.database.prepare(query).run(northId, southId); } - /** - * Delete an external subscription for a given North connector - */ - deleteExternalNorthSubscription(northId: string, externalSourceId: string): void { - const query = `DELETE FROM ${EXTERNAL_SUBSCRIPTION_TABLE} WHERE north_connector_id = ? AND external_source_id = ?;`; - this.database.prepare(query).run(northId, externalSourceId); - } - /** * Delete all subscriptions for a given North connector */ @@ -98,12 +62,4 @@ export default class SubscriptionRepository { const query = `DELETE FROM ${SUBSCRIPTION_TABLE} WHERE north_connector_id = ?;`; this.database.prepare(query).run(northId); } - - /** - * Delete all external subscriptions for a given North connector - */ - deleteExternalNorthSubscriptions(northId: string): void { - const query = `DELETE FROM ${EXTERNAL_SUBSCRIPTION_TABLE} WHERE north_connector_id = ?;`; - this.database.prepare(query).run(northId); - } } diff --git a/backend/src/service/reload.service.spec.ts b/backend/src/service/reload.service.spec.ts index ef8a119f58..3872f0832c 100644 --- a/backend/src/service/reload.service.spec.ts +++ b/backend/src/service/reload.service.spec.ts @@ -576,24 +576,6 @@ describe('reload service', () => { expect(oibusEngine.startNorth).toHaveBeenCalledWith('northId', { id: 'northId', enabled: true }); }); - it('should create external North subscription', async () => { - (repositoryService.northConnectorRepository.getNorthConnector as jest.Mock).mockReturnValue(null); - - await service.onCreateExternalNorthSubscription('northId', 'externalId'); - expect(oibusEngine.stopNorth).toHaveBeenCalledWith('northId'); - expect(repositoryService.subscriptionRepository.createExternalNorthSubscription).toHaveBeenCalledWith('northId', 'externalId'); - expect(oibusEngine.startNorth).not.toHaveBeenCalled(); - }); - - it('should create external North subscription and restart', async () => { - (repositoryService.northConnectorRepository.getNorthConnector as jest.Mock).mockReturnValue({ id: 'northId', enabled: true }); - - await service.onCreateExternalNorthSubscription('northId', 'externalId'); - expect(oibusEngine.stopNorth).toHaveBeenCalledWith('northId'); - expect(repositoryService.subscriptionRepository.createExternalNorthSubscription).toHaveBeenCalledWith('northId', 'externalId'); - expect(oibusEngine.startNorth).toHaveBeenCalledWith('northId', { id: 'northId', enabled: true }); - }); - it('should delete North subscription', async () => { (repositoryService.northConnectorRepository.getNorthConnector as jest.Mock).mockReturnValue(null); @@ -612,24 +594,6 @@ describe('reload service', () => { expect(oibusEngine.startNorth).toHaveBeenCalledWith('northId', { id: 'northId', enabled: true }); }); - it('should delete external North subscription', async () => { - (repositoryService.northConnectorRepository.getNorthConnector as jest.Mock).mockReturnValue(null); - - await service.onDeleteExternalNorthSubscription('northId', 'externalId'); - expect(oibusEngine.stopNorth).toHaveBeenCalledWith('northId'); - expect(repositoryService.subscriptionRepository.deleteExternalNorthSubscription).toHaveBeenCalledWith('northId', 'externalId'); - expect(oibusEngine.startNorth).not.toHaveBeenCalled(); - }); - - it('should delete external North subscription and restart', async () => { - (repositoryService.northConnectorRepository.getNorthConnector as jest.Mock).mockReturnValue({ id: 'northId', enabled: true }); - - await service.onDeleteExternalNorthSubscription('northId', 'externalId'); - expect(oibusEngine.stopNorth).toHaveBeenCalledWith('northId'); - expect(repositoryService.subscriptionRepository.deleteExternalNorthSubscription).toHaveBeenCalledWith('northId', 'externalId'); - expect(oibusEngine.startNorth).toHaveBeenCalledWith('northId', { id: 'northId', enabled: true }); - }); - it('should retrieve error file from north', async () => { await service.getErrorFiles('northId', '2020-02-02T02:02:02.222Z', '2022-02-02T02:02:02.222Z', 'file'); expect(oibusEngine.getErrorFiles).toHaveBeenCalledWith('northId', '2020-02-02T02:02:02.222Z', '2022-02-02T02:02:02.222Z', 'file'); diff --git a/backend/src/service/reload.service.ts b/backend/src/service/reload.service.ts index 76219f99b5..db8dcb9f88 100644 --- a/backend/src/service/reload.service.ts +++ b/backend/src/service/reload.service.ts @@ -286,15 +286,6 @@ export default class ReloadService { } } - async onCreateExternalNorthSubscription(northId: string, externalSourceId: string): Promise { - await this.oibusEngine.stopNorth(northId); - this.repositoryService.subscriptionRepository.createExternalNorthSubscription(northId, externalSourceId); - const settings = this.repositoryService.northConnectorRepository.getNorthConnector(northId); - if (settings && settings.enabled) { - await this.oibusEngine.startNorth(northId, settings); - } - } - async onDeleteNorthSubscription(northId: string, southId: string): Promise { await this.oibusEngine.stopNorth(northId); this.repositoryService.subscriptionRepository.deleteNorthSubscription(northId, southId); @@ -304,15 +295,6 @@ export default class ReloadService { } } - async onDeleteExternalNorthSubscription(northId: string, externalSourceId: string): Promise { - await this.oibusEngine.stopNorth(northId); - this.repositoryService.subscriptionRepository.deleteExternalNorthSubscription(northId, externalSourceId); - const settings = this.repositoryService.northConnectorRepository.getNorthConnector(northId); - if (settings && settings.enabled) { - await this.oibusEngine.startNorth(northId, settings); - } - } - async onCreateHistoryQuery(command: HistoryQueryCommandDTO, southItems: Array): Promise { const historyQuery = this.repositoryService.historyQueryRepository.createHistoryQuery(command); for (const item of southItems) { diff --git a/backend/src/service/repository.service.spec.ts b/backend/src/service/repository.service.spec.ts index 97394f34eb..b8aded046e 100644 --- a/backend/src/service/repository.service.spec.ts +++ b/backend/src/service/repository.service.spec.ts @@ -3,7 +3,6 @@ import db from 'better-sqlite3'; import RepositoryService from './repository.service'; import EngineRepository from '../repository/engine.repository'; -import ExternalSourceRepository from '../repository/external-source.repository'; import IpFilterRepository from '../repository/ip-filter.repository'; import ScanModeRepository from '../repository/scan-mode.repository'; import SouthConnectorRepository from '../repository/south-connector.repository'; @@ -24,7 +23,6 @@ import RegistrationRepository from '../repository/registration.repository'; import CommandRepository from '../repository/command.repository'; jest.mock('better-sqlite3', () => jest.fn(() => 'sqlite database')); -jest.mock('../repository/external-source.repository'); jest.mock('../repository/crypto.repository'); jest.mock('../repository/ip-filter.repository'); jest.mock('../repository/scan-mode.repository'); @@ -54,7 +52,6 @@ describe('Repository service', () => { expect(db).toHaveBeenCalledWith('myCacheDatabase'); expect(EngineRepository).toHaveBeenCalledWith('sqlite database'); expect(CryptoRepository).toHaveBeenCalledWith('sqlite database'); - expect(ExternalSourceRepository).toHaveBeenCalledWith('sqlite database'); expect(IpFilterRepository).toHaveBeenCalledWith('sqlite database'); expect(ScanModeRepository).toHaveBeenCalledWith('sqlite database'); expect(NorthConnectorRepository).toHaveBeenCalledWith('sqlite database'); @@ -75,7 +72,6 @@ describe('Repository service', () => { expect(repositoryService.engineRepository).toBeDefined(); expect(repositoryService.cryptoRepository).toBeDefined(); - expect(repositoryService.externalSourceRepository).toBeDefined(); expect(repositoryService.ipFilterRepository).toBeDefined(); expect(repositoryService.scanModeRepository).toBeDefined(); expect(repositoryService.northConnectorRepository).toBeDefined(); diff --git a/backend/src/service/repository.service.ts b/backend/src/service/repository.service.ts index 3c754b5aab..c1189bd328 100644 --- a/backend/src/service/repository.service.ts +++ b/backend/src/service/repository.service.ts @@ -1,7 +1,6 @@ import Database from 'better-sqlite3'; import EngineRepository from '../repository/engine.repository'; -import ExternalSourceRepository from '../repository/external-source.repository'; import IpFilterRepository from '../repository/ip-filter.repository'; import ScanModeRepository from '../repository/scan-mode.repository'; import SouthConnectorRepository from '../repository/south-connector.repository'; @@ -24,7 +23,6 @@ import CommandRepository from '../repository/command.repository'; export default class RepositoryService { private readonly _engineRepository: EngineRepository; private readonly _cryptoRepository: CryptoRepository; - private readonly _externalSourceRepository: ExternalSourceRepository; private readonly _ipFilterRepository: IpFilterRepository; private readonly _scanModeRepository: ScanModeRepository; private readonly _certificateRepository: CertificateRepository; @@ -49,7 +47,6 @@ export default class RepositoryService { const cryptoDatabase = Database(cryptoDatabasePath); const cacheDatabase = Database(cacheDatabasePath); - this._externalSourceRepository = new ExternalSourceRepository(oibusDatabase); this._ipFilterRepository = new IpFilterRepository(oibusDatabase); this._scanModeRepository = new ScanModeRepository(oibusDatabase); this._certificateRepository = new CertificateRepository(oibusDatabase); @@ -112,10 +109,6 @@ export default class RepositoryService { return this._engineRepository; } - get externalSourceRepository(): ExternalSourceRepository { - return this._externalSourceRepository; - } - get ipFilterRepository(): IpFilterRepository { return this._ipFilterRepository; } diff --git a/backend/src/tests/__mocks__/repository-service.mock.ts b/backend/src/tests/__mocks__/repository-service.mock.ts index d8f7211943..84ebca8233 100644 --- a/backend/src/tests/__mocks__/repository-service.mock.ts +++ b/backend/src/tests/__mocks__/repository-service.mock.ts @@ -134,16 +134,11 @@ export default jest.fn().mockImplementation(() => ({ }, subscriptionRepository: { getNorthSubscriptions: jest.fn(), - getExternalNorthSubscriptions: jest.fn(), getSubscribedNorthConnectors: jest.fn(), checkNorthSubscription: jest.fn(), - checkExternalNorthSubscription: jest.fn(), createNorthSubscription: jest.fn(), - createExternalNorthSubscription: jest.fn(), deleteNorthSubscription: jest.fn(), - deleteExternalNorthSubscription: jest.fn(), - deleteNorthSubscriptions: jest.fn(), - deleteExternalNorthSubscriptions: jest.fn() + deleteNorthSubscriptions: jest.fn() }, southMetricsRepository: new SouthMetricsRepositoryMock(), northMetricsRepository: new NorthMetricsRepositoryMock(), diff --git a/backend/src/web-server/controllers/content.controller.spec.ts b/backend/src/web-server/controllers/content.controller.spec.ts index e3eca137ea..c38d733c38 100644 --- a/backend/src/web-server/controllers/content.controller.spec.ts +++ b/backend/src/web-server/controllers/content.controller.spec.ts @@ -32,47 +32,18 @@ describe('Content controller', () => { it('should add values', async () => { ctx.request.body = content; - ctx.request.query = { name: 'source', northId: 'northId' }; - ctx.app.repositoryService.externalSourceRepository.findExternalSourceByReference.mockReturnValue({ - id: '1', - reference: 'source', - description: 'description' - }); + ctx.request.query = { northId: 'northId' }; await oibusController.addContent(ctx); expect(ctx.noContent).toHaveBeenCalled(); expect(ctx.app.oibusService.addExternalContent).toHaveBeenCalledWith('northId', content); }); - it('should add values with null as external source', async () => { - ctx.request.body = content; - ctx.request.query = { name: 'source', northId: 'northId' }; - ctx.app.repositoryService.externalSourceRepository.findExternalSourceByReference.mockReturnValue(null); - await oibusController.addContent(ctx); - expect(ctx.badRequest).toHaveBeenCalled(); - expect(ctx.noContent).not.toHaveBeenCalled(); - expect(ctx.app.oibusService.addExternalContent).not.toHaveBeenCalled(); - }); - - it('should not add values if no source name provided', async () => { - ctx.request.body = content; - ctx.request.query = { northId: 'northId' }; - - await oibusController.addContent(ctx); - expect(ctx.badRequest).toHaveBeenCalled(); - expect(ctx.app.oibusService.addExternalContent).not.toHaveBeenCalled(); - }); - it('should properly manage internal error when adding values', async () => { (ctx.app.oibusService.addExternalContent as jest.Mock).mockImplementationOnce(() => { throw new Error('internal error'); }); - ctx.app.repositoryService.externalSourceRepository.findExternalSourceByReference.mockReturnValue({ - id: '1', - reference: 'source', - description: 'description' - }); ctx.request.body = content; - ctx.request.query = { name: 'source', northId: ['northId1', 'northId2'] }; + ctx.request.query = { northId: ['northId1', 'northId2'] }; await oibusController.addContent(ctx); expect(ctx.internalServerError).toHaveBeenCalled(); expect(ctx.badRequest).not.toHaveBeenCalled(); @@ -80,14 +51,9 @@ describe('Content controller', () => { }); it('should add file', async () => { - ctx.request.query = { name: 'source', northId: 'northId' }; + ctx.request.query = { northId: 'northId' }; ctx.request.body = fileContent; ctx.request.file = { path: 'filePath' }; - ctx.app.repositoryService.externalSourceRepository.findExternalSourceByReference.mockReturnValue({ - id: '1', - reference: 'source', - description: 'description' - }); await oibusController.addContent(ctx); expect(ctx.noContent).toHaveBeenCalled(); expect(ctx.app.oibusService.addExternalContent).toHaveBeenCalledWith('northId', { type: 'raw', filePath: 'filePath' }); diff --git a/backend/src/web-server/controllers/content.controller.ts b/backend/src/web-server/controllers/content.controller.ts index 1ec971e4b8..505104876f 100644 --- a/backend/src/web-server/controllers/content.controller.ts +++ b/backend/src/web-server/controllers/content.controller.ts @@ -4,15 +4,7 @@ import AbstractController from './abstract.controller'; export default class ContentController extends AbstractController { async addContent(ctx: KoaContext): Promise { - const { name, northId } = ctx.request.query; - if (!name) { - return ctx.badRequest(); - } - const externalSource = ctx.app.repositoryService.externalSourceRepository.findExternalSourceByReference(name as string); - if (!externalSource) { - ctx.app.logger.info(`External source "${name}" not found`); - return ctx.badRequest(); - } + const { northId } = ctx.request.query; const ids = []; if (typeof northId === 'string') { diff --git a/backend/src/web-server/controllers/external-source.controller.spec.ts b/backend/src/web-server/controllers/external-source.controller.spec.ts deleted file mode 100644 index 18335ecc6c..0000000000 --- a/backend/src/web-server/controllers/external-source.controller.spec.ts +++ /dev/null @@ -1,134 +0,0 @@ -import Joi from 'joi'; - -import ExternalSourceController from './external-source.controller'; -import JoiValidator from './validators/joi.validator'; -import KoaContextMock from '../../tests/__mocks__/koa-context.mock'; - -jest.mock('./validators/joi.validator'); - -const validator = new JoiValidator(); -const schema = Joi.object({}); -const externalSourceController = new ExternalSourceController(validator, schema); - -const ctx = new KoaContextMock(); -const externalSourceCommand = { - reference: 'reference', - description: 'description' -}; -const externalSource = { - id: '1', - ...externalSourceCommand -}; - -describe('External source controller', () => { - beforeEach(async () => { - jest.resetAllMocks(); - }); - - it('getExternalSources() should return external sources', async () => { - ctx.app.repositoryService.externalSourceRepository.getExternalSources.mockReturnValue([externalSource]); - - await externalSourceController.getExternalSources(ctx); - - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSources).toHaveBeenCalled(); - expect(ctx.ok).toHaveBeenCalledWith([externalSource]); - }); - - it('getExternalSource() should return external source', async () => { - const id = 'id'; - - ctx.params.id = id; - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue(externalSource); - - await externalSourceController.getExternalSource(ctx); - - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(id); - expect(ctx.ok).toHaveBeenCalledWith(externalSource); - }); - - it('getExternalSource() should return not found', async () => { - const id = 'id'; - ctx.params.id = id; - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue(null); - - await externalSourceController.getExternalSource(ctx); - - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(id); - expect(ctx.notFound).toHaveBeenCalledWith(); - }); - - it('createExternalSource() should create external source', async () => { - ctx.request.body = externalSourceCommand; - ctx.app.repositoryService.externalSourceRepository.createExternalSource.mockReturnValue(externalSource); - - await externalSourceController.createExternalSource(ctx); - - expect(validator.validate).toHaveBeenCalledWith(schema, externalSourceCommand); - expect(ctx.app.repositoryService.externalSourceRepository.createExternalSource).toHaveBeenCalledWith(externalSourceCommand); - expect(ctx.created).toHaveBeenCalledWith(externalSource); - }); - - it('createExternalSource() should return bad request', async () => { - ctx.request.body = externalSourceCommand; - const validationError = new Error('invalid body'); - validator.validate = jest.fn().mockImplementationOnce(() => { - throw validationError; - }); - - await externalSourceController.createExternalSource(ctx); - - expect(validator.validate).toHaveBeenCalledWith(schema, externalSourceCommand); - expect(ctx.app.repositoryService.externalSourceRepository.createExternalSource).not.toHaveBeenCalledWith(); - expect(ctx.badRequest).toHaveBeenCalledWith(validationError.message); - }); - - it('updateExternalSource() should update external source', async () => { - const id = 'id'; - ctx.params.id = id; - ctx.request.body = externalSourceCommand; - - await externalSourceController.updateExternalSource(ctx); - - expect(validator.validate).toHaveBeenCalledWith(schema, externalSourceCommand); - expect(ctx.app.repositoryService.externalSourceRepository.updateExternalSource).toHaveBeenCalledWith(id, externalSourceCommand); - expect(ctx.noContent).toHaveBeenCalled(); - }); - - it('updateExternalSource() should return bad request', async () => { - ctx.request.body = externalSourceCommand; - const validationError = new Error('invalid body'); - validator.validate = jest.fn().mockImplementationOnce(() => { - throw validationError; - }); - - await externalSourceController.updateExternalSource(ctx); - - expect(validator.validate).toHaveBeenCalledWith(schema, externalSourceCommand); - expect(ctx.app.repositoryService.externalSourceRepository.updateExternalSource).not.toHaveBeenCalledWith(); - expect(ctx.badRequest).toHaveBeenCalledWith(validationError.message); - }); - - it('deleteExternalSource() should delete external source', async () => { - const id = 'id'; - ctx.params.id = id; - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue(externalSource); - - await externalSourceController.deleteExternalSource(ctx); - - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(id); - expect(ctx.app.repositoryService.externalSourceRepository.deleteExternalSource).toHaveBeenCalledWith(id); - expect(ctx.noContent).toHaveBeenCalled(); - }); - - it('deleteExternalSource() should return not found', async () => { - const id = 'id'; - ctx.params.id = id; - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue(null); - - await externalSourceController.deleteExternalSource(ctx); - - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(id); - expect(ctx.app.repositoryService.externalSourceRepository.deleteExternalSource).not.toHaveBeenCalled(); - expect(ctx.notFound).toHaveBeenCalled(); - }); -}); diff --git a/backend/src/web-server/controllers/external-source.controller.ts b/backend/src/web-server/controllers/external-source.controller.ts deleted file mode 100644 index 9238f29289..0000000000 --- a/backend/src/web-server/controllers/external-source.controller.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { KoaContext } from '../koa'; -import { ExternalSourceCommandDTO, ExternalSourceDTO } from '../../../../shared/model/external-sources.model'; -import AbstractController from './abstract.controller'; - -export default class ExternalSourceController extends AbstractController { - async getExternalSources(ctx: KoaContext>): Promise { - const externalSources = ctx.app.repositoryService.externalSourceRepository.getExternalSources(); - ctx.ok(externalSources); - } - - async getExternalSource(ctx: KoaContext): Promise { - const externalSource = ctx.app.repositoryService.externalSourceRepository.getExternalSource(ctx.params.id); - if (externalSource) { - ctx.ok(externalSource); - } else { - ctx.notFound(); - } - } - - async createExternalSource(ctx: KoaContext): Promise { - try { - await this.validate(ctx.request.body); - const externalSource = ctx.app.repositoryService.externalSourceRepository.createExternalSource( - ctx.request.body as ExternalSourceCommandDTO - ); - ctx.created(externalSource); - } catch (error: any) { - ctx.badRequest(error.message); - } - } - - async updateExternalSource(ctx: KoaContext): Promise { - try { - await this.validate(ctx.request.body); - ctx.app.repositoryService.externalSourceRepository.updateExternalSource(ctx.params.id, ctx.request.body as ExternalSourceCommandDTO); - ctx.noContent(); - } catch (error: any) { - ctx.badRequest(error.message); - } - } - - async deleteExternalSource(ctx: KoaContext): Promise { - const externalSource = ctx.app.repositoryService.externalSourceRepository.getExternalSource(ctx.params.id); - if (externalSource) { - ctx.app.repositoryService.externalSourceRepository.deleteExternalSource(ctx.params.id); - ctx.noContent(); - } else { - ctx.notFound(); - } - } -} diff --git a/backend/src/web-server/controllers/north-connector.controller.spec.ts b/backend/src/web-server/controllers/north-connector.controller.spec.ts index 6266f8a5d7..f2bad7d4d0 100644 --- a/backend/src/web-server/controllers/north-connector.controller.spec.ts +++ b/backend/src/web-server/controllers/north-connector.controller.spec.ts @@ -280,19 +280,13 @@ describe('North connector controller', () => { north: { ...northConnectorCommand }, subscriptions: [ { type: 'south', subscription: { id: 'id1' } }, - { type: 'south', subscription: { id: 'id2' } }, - { type: 'external-source', externalSubscription: { id: 'id3' } }, - { type: 'external-source', externalSubscription: { id: 'id4' } } + { type: 'south', subscription: { id: 'id2' } } ], - subscriptionsToDelete: [ - { type: 'south', subscription: { id: 'id5' } }, - { type: 'external-source', externalSubscription: { id: 'id6' } } - ] + subscriptionsToDelete: [{ type: 'south', subscription: { id: 'id5' } }] }; ctx.params.id = 'id'; ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue(northConnector); ctx.app.repositoryService.subscriptionRepository.getNorthSubscriptions.mockReturnValue(['id1']); - ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions.mockReturnValue(['id3']); ctx.app.encryptionService.encryptConnectorSecrets.mockReturnValue(northConnectorCommand.settings); await northConnectorController.updateNorthConnector(ctx); @@ -300,16 +294,13 @@ describe('North connector controller', () => { expect(validator.validateSettings).toHaveBeenCalledWith(northTestManifest.settings, northConnectorCommand.settings); expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith('id'); expect(ctx.app.repositoryService.subscriptionRepository.getNorthSubscriptions).toHaveBeenCalledWith('id'); - expect(ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions).toHaveBeenCalledWith('id'); expect(ctx.app.encryptionService.encryptConnectorSecrets).toHaveBeenCalledWith( northConnectorCommand.settings, northConnector.settings, northTestManifest.settings ); expect(ctx.app.repositoryService.subscriptionRepository.createNorthSubscription).toHaveBeenCalledWith('id', 'id2'); - expect(ctx.app.repositoryService.subscriptionRepository.createExternalNorthSubscription).toHaveBeenCalledWith('id', 'id4'); expect(ctx.app.repositoryService.subscriptionRepository.deleteNorthSubscription).toHaveBeenCalledWith('id', 'id5'); - expect(ctx.app.repositoryService.subscriptionRepository.deleteExternalNorthSubscription).toHaveBeenCalledWith('id', 'id6'); expect(ctx.app.reloadService.onUpdateNorthSettings).toHaveBeenCalledWith('id', northConnectorCommand); expect(ctx.noContent).toHaveBeenCalled(); }); @@ -389,7 +380,6 @@ describe('North connector controller', () => { await northConnectorController.deleteNorthConnector(ctx); expect(ctx.app.repositoryService.subscriptionRepository.deleteNorthSubscriptions).toHaveBeenCalledWith('id'); - expect(ctx.app.repositoryService.subscriptionRepository.deleteExternalNorthSubscriptions).toHaveBeenCalledWith('id'); expect(ctx.app.reloadService.onDeleteNorth).toHaveBeenCalledWith('id'); expect(ctx.noContent).toHaveBeenCalled(); }); diff --git a/backend/src/web-server/controllers/north-connector.controller.ts b/backend/src/web-server/controllers/north-connector.controller.ts index 57ccbdd829..767e5a5d59 100644 --- a/backend/src/web-server/controllers/north-connector.controller.ts +++ b/backend/src/web-server/controllers/north-connector.controller.ts @@ -89,18 +89,12 @@ export default class NorthConnectorController { const subscriptionsToAdd = ctx.request.body.subscriptions .filter(element => element.type === 'south') .map(element => element.subscription!.id); - const externalSubscriptionsToAdd = ctx.request.body.subscriptions - .filter(element => element.type === 'external-source') - .map(element => element.externalSubscription!.id); const northConnector = await ctx.app.reloadService.onCreateNorth(command); for (const subscription of subscriptionsToAdd) { ctx.app.repositoryService.subscriptionRepository.createNorthSubscription(northConnector.id, subscription); } - for (const subscription of externalSubscriptionsToAdd) { - ctx.app.repositoryService.subscriptionRepository.createExternalNorthSubscription(northConnector.id, subscription); - } if (command.enabled) { await ctx.app.reloadService.onStartNorth(northConnector.id); @@ -137,7 +131,6 @@ export default class NorthConnectorController { ); const existingSubscriptions = ctx.app.repositoryService.subscriptionRepository.getNorthSubscriptions(ctx.params.id); - const existingExternalSubscriptions = ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions(ctx.params.id); const subscriptionsToAdd = ctx.request.body.subscriptions .filter( element => @@ -145,32 +138,16 @@ export default class NorthConnectorController { !existingSubscriptions.find(existingSubscription => existingSubscription === element.subscription!.id) ) .map(element => element.subscription!.id); - const externalSubscriptionsToAdd = ctx.request.body.subscriptions - .filter( - element => - element.type === 'external-source' && - !existingExternalSubscriptions.find(existingSubscription => existingSubscription === element.externalSubscription!.id) - ) - .map(element => element.externalSubscription!.id); const subscriptionsToRemove = ctx.request.body.subscriptionsToDelete .filter(element => element.type === 'south') .map(element => element.subscription!.id); - const externalSubscriptionsToRemove = ctx.request.body.subscriptionsToDelete - .filter(element => element.type === 'external-source') - .map(element => element.externalSubscription!.id); for (const subscription of subscriptionsToAdd) { ctx.app.repositoryService.subscriptionRepository.createNorthSubscription(ctx.params.id, subscription); } - for (const subscription of externalSubscriptionsToAdd) { - ctx.app.repositoryService.subscriptionRepository.createExternalNorthSubscription(ctx.params.id, subscription); - } for (const subscription of subscriptionsToRemove) { ctx.app.repositoryService.subscriptionRepository.deleteNorthSubscription(ctx.params.id, subscription); } - for (const subscription of externalSubscriptionsToRemove) { - ctx.app.repositoryService.subscriptionRepository.deleteExternalNorthSubscription(ctx.params.id, subscription); - } await ctx.app.reloadService.onUpdateNorthSettings(ctx.params.id, command); ctx.noContent(); } catch (error: any) { @@ -182,7 +159,6 @@ export default class NorthConnectorController { const northConnector = ctx.app.repositoryService.northConnectorRepository.getNorthConnector(ctx.params.id); if (northConnector) { await ctx.app.repositoryService.subscriptionRepository.deleteNorthSubscriptions(ctx.params.id); - await ctx.app.repositoryService.subscriptionRepository.deleteExternalNorthSubscriptions(ctx.params.id); await ctx.app.reloadService.onDeleteNorth(ctx.params.id); ctx.noContent(); } else { diff --git a/backend/src/web-server/controllers/subscription.controller.spec.ts b/backend/src/web-server/controllers/subscription.controller.spec.ts index a4e82188f3..01644688e4 100644 --- a/backend/src/web-server/controllers/subscription.controller.spec.ts +++ b/backend/src/web-server/controllers/subscription.controller.spec.ts @@ -23,20 +23,6 @@ describe('Subscription controller', () => { expect(ctx.ok).toHaveBeenCalledWith(['south1', 'south2']); }); - it('getExternalNorthSubscriptions() should return external subscriptions', async () => { - const northId = 'northId'; - - ctx.params.northId = northId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue({ id: northId }); - ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions.mockReturnValue(['external1', 'external2']); - - await subscriptionController.getExternalNorthSubscriptions(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions).toHaveBeenCalledWith(northId); - expect(ctx.ok).toHaveBeenCalledWith(['external1', 'external2']); - }); - it('getNorthSubscriptions() should return not found', async () => { const northId = 'northId'; @@ -50,19 +36,6 @@ describe('Subscription controller', () => { expect(ctx.notFound).toHaveBeenCalled(); }); - it('getExternalNorthSubscriptions() should return not found', async () => { - const northId = 'northId'; - - ctx.params.northId = northId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue(null); - - await subscriptionController.getExternalNorthSubscriptions(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions).not.toHaveBeenCalled(); - expect(ctx.notFound).toHaveBeenCalled(); - }); - it('createNorthSubscription() should create subscription', async () => { const northId = 'northId'; const southId = 'southId'; @@ -136,79 +109,6 @@ describe('Subscription controller', () => { expect(ctx.throw).toHaveBeenCalledWith(409, 'Subscription already exists'); }); - it('createExternalNorthSubscription() should create subscription', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue({ id: northId }); - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue({ id: externalSourceId }); - ctx.app.repositoryService.subscriptionRepository.checkExternalNorthSubscription.mockReturnValue(false); - - await subscriptionController.createExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(externalSourceId); - expect(ctx.app.repositoryService.subscriptionRepository.checkExternalNorthSubscription).toHaveBeenCalledWith(northId, externalSourceId); - expect(ctx.app.reloadService.onCreateExternalNorthSubscription).toHaveBeenCalledWith(northId, externalSourceId); - expect(ctx.noContent).toHaveBeenCalled(); - }); - - it('createExternalNorthSubscription() return not found when North is not found', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue(null); - - await subscriptionController.createExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).not.toHaveBeenCalled(); - expect(ctx.app.repositoryService.subscriptionRepository.checkExternalNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.app.reloadService.onCreateNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.notFound).toHaveBeenCalled(); - }); - - it('createExternalNorthSubscription() return not found when external south is not found', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue({ id: northId }); - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue(null); - - await subscriptionController.createExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(externalSourceId); - expect(ctx.app.repositoryService.subscriptionRepository.checkNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.app.reloadService.onCreateNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.notFound).toHaveBeenCalled(); - }); - - it('createExternalNorthSubscription() return bad request when subscription already exists', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue({ id: northId }); - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue({ id: externalSourceId }); - ctx.app.repositoryService.subscriptionRepository.checkExternalNorthSubscription.mockReturnValue(true); - - await subscriptionController.createExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(externalSourceId); - expect(ctx.app.repositoryService.subscriptionRepository.checkExternalNorthSubscription).toHaveBeenCalledWith(northId, externalSourceId); - expect(ctx.app.reloadService.onCreateNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.throw).toHaveBeenCalledWith(409, 'Subscription already exists'); - }); - it('deleteNorthSubscription() should delete subscription', async () => { const northId = 'northId'; const southId = 'southId'; @@ -258,54 +158,4 @@ describe('Subscription controller', () => { expect(ctx.app.reloadService.onDeleteNorthSubscription).not.toHaveBeenCalled(); expect(ctx.notFound).toHaveBeenCalled(); }); - - it('deleteExternalNorthSubscription() should delete external subscription', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue({ id: northId }); - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue({ id: externalSourceId }); - - await subscriptionController.deleteExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(externalSourceId); - expect(ctx.app.reloadService.onDeleteExternalNorthSubscription).toHaveBeenCalledWith(northId, externalSourceId); - expect(ctx.noContent).toHaveBeenCalled(); - }); - - it('deleteExternalNorthSubscription() should return not found when North is not found', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue(null); - - await subscriptionController.deleteExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).not.toHaveBeenCalled(); - expect(ctx.app.reloadService.onDeleteExternalNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.notFound).toHaveBeenCalled(); - }); - - it('deleteExternalNorthSubscription() should return not found when external source is not found', async () => { - const northId = 'northId'; - const externalSourceId = 'externalSourceId'; - - ctx.params.northId = northId; - ctx.params.externalSourceId = externalSourceId; - ctx.app.repositoryService.northConnectorRepository.getNorthConnector.mockReturnValue({ id: northId }); - ctx.app.repositoryService.externalSourceRepository.getExternalSource.mockReturnValue(null); - - await subscriptionController.deleteExternalNorthSubscription(ctx); - - expect(ctx.app.repositoryService.northConnectorRepository.getNorthConnector).toHaveBeenCalledWith(northId); - expect(ctx.app.repositoryService.externalSourceRepository.getExternalSource).toHaveBeenCalledWith(externalSourceId); - expect(ctx.app.reloadService.onDeleteExternalNorthSubscription).not.toHaveBeenCalled(); - expect(ctx.notFound).toHaveBeenCalled(); - }); }); diff --git a/backend/src/web-server/controllers/subscription.controller.ts b/backend/src/web-server/controllers/subscription.controller.ts index e9dc7bd896..c98688283f 100644 --- a/backend/src/web-server/controllers/subscription.controller.ts +++ b/backend/src/web-server/controllers/subscription.controller.ts @@ -1,5 +1,5 @@ import { KoaContext } from '../koa'; -import { ExternalSubscriptionDTO, SubscriptionDTO } from '../../../../shared/model/subscription.model'; +import { SubscriptionDTO } from '../../../../shared/model/subscription.model'; export default class SubscriptionController { async getNorthSubscriptions(ctx: KoaContext>): Promise { @@ -12,16 +12,6 @@ export default class SubscriptionController { ctx.ok(subscriptions); } - async getExternalNorthSubscriptions(ctx: KoaContext>): Promise { - const northConnector = ctx.app.repositoryService.northConnectorRepository.getNorthConnector(ctx.params.northId); - if (!northConnector) { - return ctx.notFound(); - } - - const subscriptions = ctx.app.repositoryService.subscriptionRepository.getExternalNorthSubscriptions(ctx.params.northId); - ctx.ok(subscriptions); - } - async createNorthSubscription(ctx: KoaContext): Promise { const northConnector = ctx.app.repositoryService.northConnectorRepository.getNorthConnector(ctx.params.northId); if (!northConnector) { @@ -41,25 +31,6 @@ export default class SubscriptionController { return ctx.noContent(); } - async createExternalNorthSubscription(ctx: KoaContext): Promise { - const northConnector = ctx.app.repositoryService.northConnectorRepository.getNorthConnector(ctx.params.northId); - if (!northConnector) { - return ctx.notFound(); - } - - const externalSource = ctx.app.repositoryService.externalSourceRepository.getExternalSource(ctx.params.externalSourceId); - if (!externalSource) { - return ctx.notFound(); - } - - if (ctx.app.repositoryService.subscriptionRepository.checkExternalNorthSubscription(ctx.params.northId, ctx.params.externalSourceId)) { - return ctx.throw(409, 'Subscription already exists'); - } - - await ctx.app.reloadService.onCreateExternalNorthSubscription(ctx.params.northId, ctx.params.externalSourceId); - return ctx.noContent(); - } - async deleteNorthSubscription(ctx: KoaContext): Promise { const northConnector = ctx.app.repositoryService.northConnectorRepository.getNorthConnector(ctx.params.northId); if (!northConnector) { @@ -74,19 +45,4 @@ export default class SubscriptionController { await ctx.app.reloadService.onDeleteNorthSubscription(ctx.params.northId, ctx.params.southId); return ctx.noContent(); } - - async deleteExternalNorthSubscription(ctx: KoaContext): Promise { - const northConnector = ctx.app.repositoryService.northConnectorRepository.getNorthConnector(ctx.params.northId); - if (!northConnector) { - return ctx.notFound(); - } - - const externalSource = ctx.app.repositoryService.externalSourceRepository.getExternalSource(ctx.params.externalSourceId); - if (!externalSource) { - return ctx.notFound(); - } - - await ctx.app.reloadService.onDeleteExternalNorthSubscription(ctx.params.northId, ctx.params.externalSourceId); - return ctx.noContent(); - } } diff --git a/backend/src/web-server/routes/index.ts b/backend/src/web-server/routes/index.ts index 8a74f6a364..30d6972bf6 100644 --- a/backend/src/web-server/routes/index.ts +++ b/backend/src/web-server/routes/index.ts @@ -4,7 +4,6 @@ import multer from '@koa/multer'; import LogController from '../controllers/log.controller'; import ScanModeController from '../controllers/scan-mode.controller'; -import ExternalSourceController from '../controllers/external-source.controller'; import EngineController from '../controllers/engine.controller'; import IpFilterController from '../controllers/ip-filter.controller'; import NorthConnectorController from '../controllers/north-connector.controller'; @@ -21,7 +20,6 @@ import { commandSchema, contentSchema, engineSchema, - externalSourceSchema, historyQuerySchema, ipFilterSchema, logSchema, @@ -36,7 +34,6 @@ const joiValidator = new JoiValidator(); const scanModeController = new ScanModeController(joiValidator, scanModeSchema); const certificateController = new CertificateController(joiValidator, certificateSchema); const commandController = new CommandController(joiValidator, commandSchema); -const externalSourceController = new ExternalSourceController(joiValidator, externalSourceSchema); const engineController = new EngineController(joiValidator, engineSchema); const contentController = new ContentController(joiValidator, contentSchema); const registrationController = new RegistrationController(joiValidator, registrationSchema); @@ -77,12 +74,6 @@ router.post('/api/certificates', (ctx: KoaContext) => certificateContr router.put('/api/certificates/:id', (ctx: KoaContext) => certificateController.update(ctx)); router.delete('/api/certificates/:id', (ctx: KoaContext) => certificateController.delete(ctx)); -router.get('/api/external-sources', (ctx: KoaContext) => externalSourceController.getExternalSources(ctx)); -router.get('/api/external-sources/:id', (ctx: KoaContext) => externalSourceController.getExternalSource(ctx)); -router.post('/api/external-sources', (ctx: KoaContext) => externalSourceController.createExternalSource(ctx)); -router.put('/api/external-sources/:id', (ctx: KoaContext) => externalSourceController.updateExternalSource(ctx)); -router.delete('/api/external-sources/:id', (ctx: KoaContext) => externalSourceController.deleteExternalSource(ctx)); - router.get('/api/engine', (ctx: KoaContext) => engineController.getEngineSettings(ctx)); router.put('/api/engine', (ctx: KoaContext) => engineController.updateEngineSettings(ctx)); router.put('/api/engine/reset-metrics', (ctx: KoaContext) => engineController.resetEngineMetrics(ctx)); @@ -119,15 +110,6 @@ router.post('/api/north/:northId/subscriptions/:southId', (ctx: KoaContext) => subscriptionController.deleteNorthSubscription(ctx) ); -router.get('/api/north/:northId/external-subscriptions', (ctx: KoaContext) => - subscriptionController.getExternalNorthSubscriptions(ctx) -); -router.post('/api/north/:northId/external-subscriptions/:externalSourceId', (ctx: KoaContext) => - subscriptionController.createExternalNorthSubscription(ctx) -); -router.delete('/api/north/:northId/external-subscriptions/:externalSourceId', (ctx: KoaContext) => - subscriptionController.deleteExternalNorthSubscription(ctx) -); router.get('/api/north/:northId/cache/file-errors', (ctx: KoaContext) => northConnectorController.getFileErrors(ctx)); router.get('/api/north/:northId/cache/file-errors/:filename', (ctx: KoaContext) => northConnectorController.getFileErrorContent(ctx) diff --git a/shared/model/external-sources.model.ts b/shared/model/external-sources.model.ts deleted file mode 100644 index a62241d36c..0000000000 --- a/shared/model/external-sources.model.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { BaseEntity } from './types'; - -/** - * DTO for external sources - * An external source is a remote data source that send data to OIBus directly to its API - */ -export interface ExternalSourceDTO extends BaseEntity { - reference: string; - description: string; -} - -/** - * Command DTO for external sources - */ -export interface ExternalSourceCommandDTO { - reference: string; - description: string; -} diff --git a/shared/model/subscription.model.ts b/shared/model/subscription.model.ts index 0dabb2da00..cb3d763c13 100644 --- a/shared/model/subscription.model.ts +++ b/shared/model/subscription.model.ts @@ -1,14 +1,11 @@ import { SouthConnectorDTO } from './south-connector.model'; -import { ExternalSourceDTO } from './external-sources.model'; /** * DTO for subscriptions */ export type SubscriptionDTO = string; -export type ExternalSubscriptionDTO = string; export interface OIBusSubscription { type: 'south' | 'external-source'; - externalSubscription?: ExternalSourceDTO; subscription?: SouthConnectorDTO; }