Skip to content

Commit

Permalink
fix(history-query): add history query tests and remove create full co…
Browse files Browse the repository at this point in the history
…nfig message
  • Loading branch information
burgerni10 committed Jan 6, 2025
1 parent 4d2ceb1 commit 644a259
Show file tree
Hide file tree
Showing 8 changed files with 1,152 additions and 45 deletions.
772 changes: 767 additions & 5 deletions backend/src/service/history-query.service.spec.ts

Large diffs are not rendered by default.

60 changes: 36 additions & 24 deletions backend/src/service/history-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ export default class HistoryQueryService {
): Promise<HistoryQueryEntity<S, N, I>> {
const southManifest = this.southService.getInstalledSouthManifests().find(southManifest => southManifest.id === command.southType);
if (!southManifest) {
throw new Error('South manifest does not exist');
throw new Error(`South manifest ${command.southType} does not exist`);
}
const northManifest = this.northService.getInstalledNorthManifests().find(southManifest => southManifest.id === command.northType);
if (!northManifest) {
throw new Error('North manifest does not exist');
throw new Error(`North manifest ${command.northType} does not exist`);
}
await this.validator.validateSettings(northManifest.settings, command.northSettings);
await this.validator.validateSettings(southManifest.settings, command.southSettings);
Expand Down Expand Up @@ -303,7 +303,7 @@ export default class HistoryQueryService {
}
const manifest = this.southService.getInstalledSouthManifests().find(southManifest => southManifest.id === historyQuery.southType);
if (!manifest) {
throw new Error('South manifest does not exist');
throw new Error(`South manifest does not exist for type ${historyQuery.southType}`);
}
await this.validator.validateSettings(manifest.items.settings, command.settings);

Expand All @@ -330,15 +330,15 @@ export default class HistoryQueryService {
): Promise<void> {
const previousSettings = this.historyQueryRepository.findHistoryQueryItemById<I>(historyQueryId, historyQueryItemId);
if (!previousSettings) {
throw new Error(`History query item ${historyQueryItemId} not found`);
throw new Error(`History query item with ID ${historyQueryItemId} does not exist`);
}
const historyQuery = this.historyQueryRepository.findHistoryQueryById(historyQueryId);
if (!historyQuery) {
throw new Error(`History Query ${historyQueryId} does not exist`);
throw new Error(`History query ${historyQueryId} does not exist`);
}
const manifest = this.southService.getInstalledSouthManifests().find(southManifest => southManifest.id === historyQuery.southType);
if (!manifest) {
throw new Error('South manifest does not exist');
throw new Error(`South manifest does not exist for type ${historyQuery.southType}`);
}
await this.validator.validateSettings(manifest.items.settings, command.settings);

Expand All @@ -351,7 +351,6 @@ export default class HistoryQueryService {
this.encryptionService
);
this.historyQueryRepository.saveHistoryQueryItem<I>(historyQuery.id, historyQueryItemEntity);
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
await this.historyQueryEngine.reloadHistoryQuery(historyQuery, false);
}

Expand All @@ -361,10 +360,9 @@ export default class HistoryQueryService {
throw new Error(`History query ${historyQueryId} does not exist`);
}
const historyQueryItem = this.historyQueryRepository.findHistoryQueryItemById(historyQueryId, historyQueryItemId);
if (!historyQueryItem) throw new Error('History Query item not found');
if (!historyQueryItem) throw new Error(`History query item ${historyQueryItemId} not found`);

this.historyQueryRepository.deleteHistoryQueryItem(historyQueryItem.id);
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
await this.historyQueryEngine.reloadHistoryQuery(historyQuery, false);
}

Expand All @@ -374,7 +372,6 @@ export default class HistoryQueryService {
throw new Error(`History query ${historyQueryId} not found`);
}
this.historyQueryRepository.deleteAllHistoryQueryItemsByHistoryQuery(historyQueryId);
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
await this.historyQueryEngine.reloadHistoryQuery(historyQuery, true);
}

Expand All @@ -385,7 +382,6 @@ export default class HistoryQueryService {
}

this.historyQueryRepository.enableHistoryQueryItem(historyQueryItem.id);
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
await this.historyQueryEngine.reloadHistoryQuery(this.historyQueryRepository.findHistoryQueryById(historyQueryId)!, false);
}

Expand All @@ -395,27 +391,41 @@ export default class HistoryQueryService {
throw new Error(`History query item ${historyQueryItemId} not found`);
}

this.historyQueryRepository.disableHistoryQueryItem(historyQueryItemId);
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
this.historyQueryRepository.disableHistoryQueryItem(historyQueryItem.id);
await this.historyQueryEngine.reloadHistoryQuery(this.historyQueryRepository.findHistoryQueryById(historyQueryId)!, false);
}

async checkCsvImport<I extends SouthItemSettings>(
async checkCsvFileImport<I extends SouthItemSettings>(
southType: string,
file: multer.File,
delimiter: string,
existingItems: Array<HistoryQueryItemDTO<I> | HistoryQueryItemCommandDTO<I>>
) {
): Promise<{
items: Array<HistoryQueryItemCommandDTO<SouthItemSettings>>;
errors: Array<{ item: HistoryQueryItemCommandDTO<SouthItemSettings>; error: string }>;
}> {
const fileContent = await fs.readFile(file.path);
return await this.checkCsvContentImport(southType, fileContent.toString('utf8'), delimiter, existingItems);
}

async checkCsvContentImport<I extends SouthItemSettings>(
southType: string,
fileContent: string,
delimiter: string,
existingItems: Array<HistoryQueryItemDTO<I> | HistoryQueryItemCommandDTO<I>>
): Promise<{
items: Array<HistoryQueryItemCommandDTO<SouthItemSettings>>;
errors: Array<{ item: HistoryQueryItemCommandDTO<SouthItemSettings>; error: string }>;
}> {
const manifest = this.southService.getInstalledSouthManifests().find(southManifest => southManifest.id === southType);
if (!manifest) {
throw new Error('South manifest not found');
throw new Error(`South manifest does not exist for type ${southType}`);
}

const fileContent = await fs.readFile(file.path);
const csvContent = csv.parse(fileContent.toString('utf8'), { header: true, delimiter });
const csvContent = csv.parse(fileContent, { header: true, delimiter });

if (csvContent.meta.delimiter !== delimiter) {
throw new Error('The entered delimiter does not correspond to the file delimiter');
throw new Error(`The entered delimiter "${delimiter}" does not correspond to the file delimiter "${csvContent.meta.delimiter}"`);
}

const validItems: Array<HistoryQueryItemCommandDTO<I>> = [];
Expand All @@ -428,7 +438,10 @@ export default class HistoryQueryService {
settings: {} as I
};
if (existingItems.find(existingItem => existingItem.name === item.name)) {
errors.push({ item, error: `Item name "${(data as Record<string, string>).name}" already used` });
errors.push({
item: item,
error: `Item name "${(data as unknown as Record<string, string>).name}" already used`
});
continue;
}

Expand Down Expand Up @@ -473,7 +486,7 @@ export default class HistoryQueryService {
) {
const historyQuery = this.historyQueryRepository.findHistoryQueryById<S, N, I>(historyQueryId);
if (!historyQuery) {
throw new Error(`History Query ${historyQueryId} does not exist`);
throw new Error(`History query ${historyQueryId} does not exist`);
}
const manifest = this.southService.getInstalledSouthManifests().find(southManifest => southManifest.id === historyQuery.southType)!;
const itemsToAdd: Array<HistoryQueryItemEntity<I>> = [];
Expand All @@ -490,7 +503,6 @@ export default class HistoryQueryService {
itemsToAdd.push(historyQueryItemEntity);
}
this.historyQueryRepository.saveAllItems<I>(historyQuery.id, itemsToAdd);
this.oIAnalyticsMessageService.createFullConfigMessageIfNotPending();
await this.historyQueryEngine.reloadHistoryQuery(historyQuery, false);
}

Expand Down Expand Up @@ -623,11 +635,11 @@ const copyHistoryQueryCommandToHistoryQueryEntity = async <S extends SouthSettin

historyQueryEntity.items = await Promise.all(
command.items.map(async itemCommand => {
const itemEntity = { id: itemCommand?.id } as HistoryQueryItemEntity<I>;
const itemEntity = { id: itemCommand.id } as HistoryQueryItemEntity<I>;
await copyHistoryQueryItemCommandToHistoryQueryItemEntity<I>(
itemEntity,
itemCommand,
historyQueryEntity.items?.find(element => element.id === itemCommand.id) || null,
currentSettings?.items.find(element => element.id === itemCommand.id) || null,
historyQueryEntity.southType,
encryptionService
);
Expand Down
5 changes: 3 additions & 2 deletions backend/src/tests/__mocks__/history-query-engine.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default jest.fn().mockImplementation(logger => {
createHistoryQuery: jest.fn(),
startHistoryQuery: jest.fn(),
stopHistoryQuery: jest.fn(),
getHistoryDataStream: jest.fn(),
deleteHistoryQuery: jest.fn()
getHistoryQueryDataStream: jest.fn(),
deleteHistoryQuery: jest.fn(),
reloadHistoryQuery: jest.fn()
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export default jest.fn().mockImplementation(() => ({
deleteAllItemsForHistoryQuery: jest.fn(),
enableHistoryQueryItem: jest.fn(),
disableHistoryQueryItem: jest.fn(),
checkCsvImport: jest.fn(),
checkCsvFileImport: jest.fn(),
importItems: jest.fn()
}));
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default jest.fn().mockImplementation(() => {
run: jest.fn(),
stop: jest.fn(),
setLogger: jest.fn(),
createFullConfigMessageIfNotPending: jest.fn()
createFullConfigMessageIfNotPending: jest.fn(),
createHistoryQueryMessage: jest.fn()
};
});
42 changes: 36 additions & 6 deletions backend/src/tests/utils/test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@ const southConnectorCommand: SouthConnectorCommandDTO<SouthSettings, SouthItemSe
id: 'newSouthItemFromConnectorId',
name: 'my new item from south connector',
enabled: true,
settings: {} as SouthItemSettings,
settings: {
regex: '*',
minAge: 100,
preserveFiles: true,
ignoreModifiedDate: false
},
scanModeId: scanModes[1].id,
scanModeName: null
}
Expand All @@ -350,7 +355,12 @@ const southConnectorItemCommand: SouthConnectorItemCommandDTO<SouthItemSettings>
scanModeId: 'scanModeId1',
scanModeName: null,
enabled: true,
settings: {} as SouthItemSettings
settings: {
regex: '*',
minAge: 100,
preserveFiles: true,
ignoreModifiedDate: false
}
};

const northTestManifest: NorthConnectorManifest = {
Expand Down Expand Up @@ -607,10 +617,30 @@ const historyQueryCommand: HistoryQueryCommandDTO<SouthSettings, NorthSettings,
description: 'description',
startTime: '2020-02-01T02:02:59.999Z',
endTime: '2020-02-02T02:02:59.999Z',
southType: 'south-test',
northType: 'north-test',
southSettings: {} as SouthSettings,
northSettings: {} as NorthSettings,
southType: 'mssql',
northType: 'file-writer',
southSettings: {
throttling: {
maxReadInterval: 3600,
readDelay: 200,
overlap: 10
},
host: 'host',
port: 1433,
connectionTimeout: 1_000,
database: 'database',
username: 'oibus',
password: 'pass',
domain: 'domain',
encryption: true,
trustServerCertificate: true,
requestTimeout: 5_000
},
northSettings: {
outputFolder: 'output-folder',
prefix: 'prefix-',
suffix: '-suffix'
},
caching: {
scanModeId: scanModes[0].id,
scanModeName: null,
Expand Down
Loading

0 comments on commit 644a259

Please sign in to comment.