From db3e1d59fd0b4cec005b0d10804d5c4bf68447c9 Mon Sep 17 00:00:00 2001 From: moeidheidari Date: Thu, 23 Jun 2022 01:54:22 +0300 Subject: [PATCH 1/2] runner script added, runbook edited --- README.md | 31 +- scripts/runner.sh | 7 + src/app/app.module.ts | 8 +- src/cli.ts | 29 +- src/parser/command/parser.command.ts | 38 +-- src/parser/common/index.ts | 2 +- src/parser/common/utils.ts | 18 +- src/parser/dtos/parser-command-dto.ts | 78 ++--- src/parser/enum/index.ts | 2 +- src/parser/enum/log-level.enum.ts | 36 +-- src/parser/helper/file-helper.ts | 147 +++++----- src/parser/helper/index.ts | 2 +- src/parser/model/index.ts | 2 +- src/parser/model/log.model.ts | 38 +-- src/parser/parser.module.ts | 8 +- src/parser/service/parser.service.ts | 32 ++- test/integration-tests/factories/index.ts | 2 +- .../factories/parser-command.factory.ts | 57 ++-- test/integration-tests/parser-command.spec.ts | 79 ++--- test/unit-tests/factories/helper.factory.ts | 138 ++++----- test/unit-tests/factories/index.ts | 6 +- test/unit-tests/factories/parser.factory.ts | 150 +++++----- test/unit-tests/factories/utils.factory.ts | 271 +++++++++-------- test/unit-tests/filehelper.spec.ts | 179 +++++++----- test/unit-tests/parser.service.spec.ts | 272 +++++++++++++----- test/unit-tests/utils.spec.ts | 120 ++++---- 26 files changed, 979 insertions(+), 773 deletions(-) create mode 100644 scripts/runner.sh diff --git a/README.md b/README.md index ab5cdcb..a22f885 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,31 @@ # Table of Contents - [Table of Contents](#table-of-contents) + - [Overview](#overview) + - [Provided Commands](#provided-commands) + - [Code architecture](#code-architecture) + - [source code](#source-code) + - [Service build information](#service-build-information) + - [Regular user](#regular-user) + - [Documentation](#documentation) + - [Example](#example) + - [ToDo list](#todo-list) ## Overview Log parser takes a log input file and tries to parse it to extract useful information. It also respects [General Data Protection Regulation (GDPR)](https://gdpr-info.eu/) - You can see take a look at the Full documentation [here](https://github.com/MoeidHeidari/log-parser/blob/main/full-documentation.md) + You can see take a look at the Full documentation [here](https://github.com/MoeidHeidari/log-parser/blob/main/full-documentation.md) + --- @@ -74,8 +84,20 @@ cd log-parser npm install npm run build npm run test +npm run lint npm start:{dev || debug || prod} ``` + +### using runner.sh + +```bash +bash scripts/runner.sh +... +Done! 😍 +now you can use the application by following commmand +node dist/cli.js -h +``` + test result ```bash @@ -123,8 +145,7 @@ Run Log parser ```bash npm run build -node dist/cli.js --input input.log --output log.json --log-level error,debug -Success 😀. output is written in log.json +ode dist/cli.js --input input.log --output log.json --log-level error,debug ``` output.json @@ -187,6 +208,4 @@ output.json - [ ] connect it to logstash - [ ] implement elastic search -## - - +## \ No newline at end of file diff --git a/scripts/runner.sh b/scripts/runner.sh new file mode 100644 index 0000000..0c27a73 --- /dev/null +++ b/scripts/runner.sh @@ -0,0 +1,7 @@ +npm install +npm run build +npm test +npm run lint +echo 'Done! 😍' +echo 'now you can use the application by following commmand' +echo 'node dist/cli.js -h' \ No newline at end of file diff --git a/src/app/app.module.ts b/src/app/app.module.ts index e04d146..86d293f 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -2,10 +2,8 @@ import { Module } from '@nestjs/common'; import { CommandModule } from 'nestjs-command'; import { ParserModule } from '../parser/parser.module'; - - @Module({ - imports: [CommandModule,ParserModule,], - providers: [] + imports: [CommandModule, ParserModule], + providers: [], }) -export class AppModule {} \ No newline at end of file +export class AppModule {} diff --git a/src/cli.ts b/src/cli.ts index 2b13764..a947f55 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,28 +1,25 @@ -import { NestFactory } from '@nestjs/core' -import { CommandModule, CommandService } from 'nestjs-command' -import { AppModule } from './app/app.module' +import { NestFactory } from '@nestjs/core'; +import { CommandModule, CommandService } from 'nestjs-command'; +import { AppModule } from './app/app.module'; /** * Main application entry point */ -async function bootstrap () { +async function bootstrap() { const app = await NestFactory.createApplicationContext(AppModule, { logger: ['error'], - }) + }); try { - await app - .select(CommandModule) - .get(CommandService) - .exec() - await app.close() + await app.select(CommandModule).get(CommandService).exec(); + await app.close(); } catch (error) { - console.error(error) - await app.close() - process.exit(1) + console.error(error); + await app.close(); + process.exit(1); } } /** -* Bootstraps the application -*/ -bootstrap() + * Bootstraps the application + */ +bootstrap(); diff --git a/src/parser/command/parser.command.ts b/src/parser/command/parser.command.ts index 652f30f..eecb8e3 100644 --- a/src/parser/command/parser.command.ts +++ b/src/parser/command/parser.command.ts @@ -1,19 +1,19 @@ -import { Command, Positional, Option } from 'nestjs-command' -import { Injectable } from '@nestjs/common' -import { ParserService } from '../service/parser.service' -import { ParserCommandDTO } from '../dtos/parser-command-dto' -import { validateDTO } from '../common' +import { Command, Option } from 'nestjs-command'; +import { Injectable } from '@nestjs/common'; +import { ParserService } from '../service/parser.service'; +import { ParserCommandDTO } from '../dtos/parser-command-dto'; +import { validateDTO } from '../common'; /** * Log parser command */ @Injectable() export class ParserCommand { - /** - * Constructs the Log parser command class - * @param parserService Injection of parser service - */ - constructor (private readonly parserService: ParserService) {} + /** + * Constructs the Log parser command class + * @param parserService Injection of parser service + */ + constructor(private readonly parserService: ParserService) {} /** * Parser command that takes the input log file, parses and writes the result to the output file as ajson content. @@ -26,7 +26,7 @@ export class ParserCommand { command: '*', describe: 'parses a log file', }) - async parse ( + async parse( @Option({ name: 'input', describe: 'input log file (ex: "input.log")', @@ -55,23 +55,23 @@ export class ParserCommand { log_level: string, ) { try { - let logFilter = log_level.split(',') - let command = new ParserCommandDTO({ + const logFilter = log_level.split(','); + const command = new ParserCommandDTO({ input: input, output: output, logLevel: logFilter, - }) - await validateDTO(command) + }); + await validateDTO(command); return this.parserService .parse(command) .then(() => { - console.log(`Success 😀. output is written in ${output}`) + console.log(`Success 😀. output is written in ${output}`); }) - .catch(error => { + .catch((error) => { console.error(error); - }) + }); } catch (error) { - console.log(error) + console.log(error); } } } diff --git a/src/parser/common/index.ts b/src/parser/common/index.ts index cfa334d..04bca77 100644 --- a/src/parser/common/index.ts +++ b/src/parser/common/index.ts @@ -1 +1 @@ -export * from './utils' \ No newline at end of file +export * from './utils'; diff --git a/src/parser/common/utils.ts b/src/parser/common/utils.ts index 547153a..2eb8db2 100644 --- a/src/parser/common/utils.ts +++ b/src/parser/common/utils.ts @@ -1,16 +1,14 @@ -import { NotAcceptableException } from "@nestjs/common"; -import { validate } from "class-validator"; +import { validate } from 'class-validator'; /** * validates dto and returns bad request if it is wrong * @param dto dto * @param httpResponseGenerator http response service */ - export async function validateDTO(dto: any): Promise { - const errors = await validate(dto); - - if (errors.length) throw new Error(`InvalidArgumentExcpetion ${errors}`); - - return dto; - } - \ No newline at end of file +export async function validateDTO(dto: any): Promise { + const errors = await validate(dto); + + if (errors.length) throw new Error(`InvalidArgumentExcpetion ${errors}`); + + return dto; +} diff --git a/src/parser/dtos/parser-command-dto.ts b/src/parser/dtos/parser-command-dto.ts index ad6be4f..88dcf42 100644 --- a/src/parser/dtos/parser-command-dto.ts +++ b/src/parser/dtos/parser-command-dto.ts @@ -1,5 +1,11 @@ -import { IsDefined, IsEnum, IsNotEmpty, IsOptional, IsString } from "class-validator"; -import { LogLevel } from "../enum"; +import { + IsDefined, + IsEnum, + IsNotEmpty, + IsOptional, + IsString, +} from 'class-validator'; +import { LogLevel } from '../enum'; /** * list of allowed properties @@ -8,38 +14,38 @@ const allowedProperties = ['input', 'output', 'logLevel']; /** * Log parser command DTO */ -export class ParserCommandDTO{ +export class ParserCommandDTO { /** - * addres of the log file input - */ - @IsNotEmpty() - @IsDefined() - @IsString() - input:string; - //........................................................................................................... - /** - * address of the output log file - */ - @IsNotEmpty() - @IsDefined() - @IsString() - output:string; - //........................................................................................................... - /** - * list of log levels to be filtered by the parser - */ - @IsOptional() - @IsEnum(LogLevel, { each: true }) - logLevel:LogLevel[] - //........................................................................................................... - /** - * Constructs the DTO based on given properties - * @param properties list of passed properties to the DTO - */ - constructor(properties: any = {}) { - Object.keys(properties).forEach((key: string) => { - if (allowedProperties.includes(key)) this[key as keyof this] = properties[key]; - }); - } - -} \ No newline at end of file + * addres of the log file input + */ + @IsNotEmpty() + @IsDefined() + @IsString() + input: string; + //........................................................................................................... + /** + * address of the output log file + */ + @IsNotEmpty() + @IsDefined() + @IsString() + output: string; + //........................................................................................................... + /** + * list of log levels to be filtered by the parser + */ + @IsOptional() + @IsEnum(LogLevel, { each: true }) + logLevel: LogLevel[]; + //........................................................................................................... + /** + * Constructs the DTO based on given properties + * @param properties list of passed properties to the DTO + */ + constructor(properties: any = {}) { + Object.keys(properties).forEach((key: string) => { + if (allowedProperties.includes(key)) + this[key as keyof this] = properties[key]; + }); + } +} diff --git a/src/parser/enum/index.ts b/src/parser/enum/index.ts index 1796497..9cc1154 100644 --- a/src/parser/enum/index.ts +++ b/src/parser/enum/index.ts @@ -1 +1 @@ -export * from './log-level.enum' \ No newline at end of file +export * from './log-level.enum'; diff --git a/src/parser/enum/log-level.enum.ts b/src/parser/enum/log-level.enum.ts index 25c0b0b..577bd11 100644 --- a/src/parser/enum/log-level.enum.ts +++ b/src/parser/enum/log-level.enum.ts @@ -1,21 +1,21 @@ /** * An enum that represents the log levels the parser understands */ -export enum LogLevel{ - /** - * Error log level - */ - ERROR='error', - /** - * Info log level - */ - INFO='info', - /** - * Warning log level - */ - WARN='warn', - /** - * Debug log level - */ - DEBUG='debug' -} \ No newline at end of file +export enum LogLevel { + /** + * Error log level + */ + ERROR = 'error', + /** + * Info log level + */ + INFO = 'info', + /** + * Warning log level + */ + WARN = 'warn', + /** + * Debug log level + */ + DEBUG = 'debug', +} diff --git a/src/parser/helper/file-helper.ts b/src/parser/helper/file-helper.ts index 03e90c3..dc1e6e9 100644 --- a/src/parser/helper/file-helper.ts +++ b/src/parser/helper/file-helper.ts @@ -1,75 +1,88 @@ -import { Injectable, NotAcceptableException,Logger } from "@nestjs/common"; +import { Injectable, NotAcceptableException, Logger } from '@nestjs/common'; import * as fs from 'fs'; -import * as rd from 'readline' -import { Observable, of } from "rxjs"; +import * as rd from 'readline'; +import { Observable, of } from 'rxjs'; /** * File helper class */ @Injectable() -export class FileHelper{ - - //======================================================================================================================================================================= - /** - * Reads the input log file - * @param path Address of the file to be red - * @param filter Log level filter (An array of log levels) - * @returns Promise>> - */ - async readLogFile(path: string, filter: string[]): Promise>> { - return new Promise((resolve, reject) => { - try { - let reader = rd.createInterface(fs.createReadStream(path)); - var data: Array = []; - reader.on("line", async (line: string) => { - try { - let log=await this.parsLog(line,filter); - if(log) data.push(log); - } catch (error) { - Logger.warn(`Illigal format on line ${line}`); - } - }) - reader.on("close", () => { - resolve(of(data)) - }) - } catch (error) { - Logger.error(`file not readable or it does not exists`); - reject(error); - } - }) - } - //======================================================================================================================================================================= - /** - * Writes the given data (Parsed log json array) to the output log file - * @param path address of the out put parsed log file - * @param data data to be written inside the output log file - * @returns Promise - */ - async writeLogFile(path,data:T[]):Promise{ - return new Promise(function(resolve, reject) { - fs.writeFile(path, JSON.stringify(data, null, 2), function(err) { - if (err) reject(err); - else resolve(data); - }); +export class FileHelper { + //======================================================================================================================================================================= + /** + * Reads the input log file + * @param path Address of the file to be red + * @param filter Log level filter (An array of log levels) + * @returns Promise>> + */ + async readLogFile( + path: string, + filter: string[], + ): Promise>> { + return new Promise((resolve, reject) => { + try { + const reader = rd.createInterface(fs.createReadStream(path)); + const data: Array = []; + reader.on('line', async (line: string) => { + try { + const log = await this.parsLog(line, filter); + if (log) data.push(log); + } catch (error) { + Logger.warn(`Illigal format on line ${line}`); + } }); + reader.on('close', () => { + resolve(of(data)); + }); + } catch (error) { + Logger.error(`file not readable or it does not exists`); + reject(error); + } + }); + } + //======================================================================================================================================================================= + /** + * Writes the given data (Parsed log json array) to the output log file + * @param path address of the out put parsed log file + * @param data data to be written inside the output log file + * @returns Promise + */ + async writeLogFile(path, data: T[]): Promise { + return new Promise(function (resolve, reject) { + fs.writeFile(path, JSON.stringify(data, null, 2), function (err) { + if (err) reject(err); + else resolve(data); + }); + }); + } + //======================================================================================================================================================================= + /** + * parses a given log information according to the log level (it uses different regex patterns to find desired parts of the log info) + * @param log log to be parsed + * @param filter Log level filter (An array of log levels) + * @returns Promise + */ + async parsLog(log: string, filter: string[]): Promise { + try { + const log_level: string = log + .match('\\s-\\s\\w+\\s-\\s')[0] + .replace(/\s-\s/g, ''); + if (!filter.includes(log_level) && filter.length > 0) return; + const time_stamp = new Date( + log.match( + '[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}[A-Z][0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}.[0-9]{1,3}[A-Z]', + )?.[0], + ).getTime(); + const log_info = JSON.parse(log.match('{(?:)(.*)}')[0]); + const transaction_id = log_info.transactionId; + const err = log_info.details; + return { + timestamp: time_stamp, + logLevel: log_level, + transactionId: transaction_id, + err: err, + }; + } catch (error) { + throw new NotAcceptableException(error); } - //======================================================================================================================================================================= - /** - * parses a given log information according to the log level (it uses different regex patterns to find desired parts of the log info) - * @param log log to be parsed - * @param filter Log level filter (An array of log levels) - * @returns Promise - */ - async parsLog(log: string,filter:string[]): Promise { - try { - let log_level:string = log.match('\\s-\\s\\w+\\s-\\s')[0].replace(/\s-\s/g, ""); - if(!filter.includes(log_level) && filter.length >0) return; - let time_stamp = new Date(log.match('[0-9]{1,4}\-[0-9]{1,2}\-[0-9]{1,2}[A-Z][0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}.[0-9]{1,3}[A-Z]')?.[0]).getTime(); - let log_info = JSON.parse(log.match('{(?:)(.*)}')[0]); - let transaction_id = log_info.transactionId; - let err = log_info.details; - return { timestamp: time_stamp, logLevel: log_level, transactionId: transaction_id, err: err } - } catch (error) { - throw new NotAcceptableException(error); - } - } + } } diff --git a/src/parser/helper/index.ts b/src/parser/helper/index.ts index beeefd3..ed484c9 100644 --- a/src/parser/helper/index.ts +++ b/src/parser/helper/index.ts @@ -1 +1 @@ -export * from './file-helper' \ No newline at end of file +export * from './file-helper'; diff --git a/src/parser/model/index.ts b/src/parser/model/index.ts index d6a5a86..78ee524 100644 --- a/src/parser/model/index.ts +++ b/src/parser/model/index.ts @@ -1 +1 @@ -export * from './log.model' \ No newline at end of file +export * from './log.model'; diff --git a/src/parser/model/log.model.ts b/src/parser/model/log.model.ts index f4d501c..0cc33f6 100644 --- a/src/parser/model/log.model.ts +++ b/src/parser/model/log.model.ts @@ -1,22 +1,22 @@ -import { LogLevel } from "../enum"; +import { LogLevel } from '../enum'; /** * Log model (We generate the parsed log json objects based on this model) */ -export interface LogModel{ - /** - * Timestamp of the log generated - */ - timeStamp:Date, - /** - * level of the log (error,debug,warn,info) - */ - logLevel:LogLevel - /** - * transaction id of the log - */ - transactionId:string, - /** - * error message of the log - */ - err:string -} \ No newline at end of file +export interface LogModel { + /** + * Timestamp of the log generated + */ + timeStamp: Date; + /** + * level of the log (error,debug,warn,info) + */ + logLevel: LogLevel; + /** + * transaction id of the log + */ + transactionId: string; + /** + * error message of the log + */ + err: string; +} diff --git a/src/parser/parser.module.ts b/src/parser/parser.module.ts index 2d2baf1..b78d2d0 100644 --- a/src/parser/parser.module.ts +++ b/src/parser/parser.module.ts @@ -1,7 +1,7 @@ -import { Module } from '@nestjs/common' -import { ParserCommand } from './command/parser.command' -import { FileHelper } from './helper' -import { ParserService } from './service/parser.service' +import { Module } from '@nestjs/common'; +import { ParserCommand } from './command/parser.command'; +import { FileHelper } from './helper'; +import { ParserService } from './service/parser.service'; /** * Parser module diff --git a/src/parser/service/parser.service.ts b/src/parser/service/parser.service.ts index 7f03457..2a54ac7 100644 --- a/src/parser/service/parser.service.ts +++ b/src/parser/service/parser.service.ts @@ -1,8 +1,8 @@ -import { Inject, Injectable, NotAcceptableException } from '@nestjs/common' -import { FileHelper } from '../helper' -import { take } from 'rxjs' -import { LogModel } from '../model' -import { ParserCommandDTO } from '../dtos/parser-command-dto' +import { Inject, Injectable, NotAcceptableException } from '@nestjs/common'; +import { FileHelper } from '../helper'; +import { take } from 'rxjs'; +import { LogModel } from '../model'; +import { ParserCommandDTO } from '../dtos/parser-command-dto'; /** * Log parser service @@ -13,17 +13,19 @@ export class ParserService { *Constructs the log parser service * @param file_helper file helper injection (useful to work with the log files) represented as a single tone class */ - constructor (@Inject('FILE_HELPER') private readonly file_helper: FileHelper){} + constructor( + @Inject('FILE_HELPER') private readonly file_helper: FileHelper, + ) {} //============================================================================================================== /** * Parses the input log file based on the provided commands * @param command Command entered by the user (Comming from Command class) * @returns Promise */ - async parse (command: ParserCommandDTO): Promise { + async parse(command: ParserCommandDTO): Promise { return new Promise(async (resolve, reject) => { try { - ;( + ( await this.file_helper.readLogFile( command.input, command.logLevel, @@ -31,14 +33,14 @@ export class ParserService { ) .pipe(take(1)) .subscribe(async (data: any) => { - await this.file_helper.writeLogFile(command.output, data) - resolve(data) - }) + await this.file_helper.writeLogFile(command.output, data); + resolve(data); + }); } catch (error) { - reject(error) + reject(error); } - }).catch(error => { - throw new NotAcceptableException(error) - }) + }).catch((error) => { + throw new NotAcceptableException(error); + }); } } diff --git a/test/integration-tests/factories/index.ts b/test/integration-tests/factories/index.ts index b5b9d1a..e68b3df 100644 --- a/test/integration-tests/factories/index.ts +++ b/test/integration-tests/factories/index.ts @@ -1 +1 @@ -export * from './parser-command.factory' \ No newline at end of file +export * from './parser-command.factory'; diff --git a/test/integration-tests/factories/parser-command.factory.ts b/test/integration-tests/factories/parser-command.factory.ts index f3e6f74..600af02 100644 --- a/test/integration-tests/factories/parser-command.factory.ts +++ b/test/integration-tests/factories/parser-command.factory.ts @@ -1,46 +1,45 @@ -import * as path from 'path' -import faker, { datatype } from 'faker'; +import { datatype } from 'faker'; +import * as path from 'path'; export const FIXTURE_BASE_PATH = path.resolve(__dirname, '../fixtures'); //===================================================================================== export const PARSE_COMMAND_INTEGRATION_TEST_CASE_1 = { data: { comman: '*', args: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', 'log-level': 'debug', }, }, expectation: { should: 'should write parssed logs to the file', }, -} +}; //===================================================================================== export const PARSE_COMMAND_INTEGRATION_TEST_CASE_2 = { - data: { - comman: '*', - args: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - 'log-level': 'error', - }, - }, - expectation: { - should: 'should write parssed logs to the file', + data: { + comman: '*', + args: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + 'log-level': 'error', }, - } - //===================================================================================== + }, + expectation: { + should: 'should write parssed logs to the file', + }, +}; +//===================================================================================== export const PARSE_COMMAND_INTEGRATION_TEST_CASE_3 = { - data: { - comman: '*', - args: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - 'log-level': datatype.string(10), - }, - }, - expectation: { - should: 'should write parssed logs to the file', + data: { + comman: '*', + args: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + 'log-level': datatype.string(10), }, - } - \ No newline at end of file + }, + expectation: { + should: 'should write parssed logs to the file', + }, +}; diff --git a/test/integration-tests/parser-command.spec.ts b/test/integration-tests/parser-command.spec.ts index ff18b0f..d1f0274 100644 --- a/test/integration-tests/parser-command.spec.ts +++ b/test/integration-tests/parser-command.spec.ts @@ -1,68 +1,73 @@ -import { CommandModule, CommandModuleTest } from 'nestjs-command' -import { AppModule } from '../../src/app/app.module' -import { Test } from '@nestjs/testing' +import { CommandModule, CommandModuleTest } from 'nestjs-command'; +import { AppModule } from '../../src/app/app.module'; +import { Test } from '@nestjs/testing'; import { FIXTURE_BASE_PATH, PARSE_COMMAND_INTEGRATION_TEST_CASE_1, PARSE_COMMAND_INTEGRATION_TEST_CASE_2, PARSE_COMMAND_INTEGRATION_TEST_CASE_3, -} from './factories' -import * as fs from 'fs' +} from './factories'; +import * as fs from 'fs'; describe('AppModule', () => { - let commandModule: CommandModuleTest - let app - async function readFile (path: string): Promise { + let commandModule: CommandModuleTest; + let app; + async function readFile(path: string): Promise { return new Promise(function (resolve, reject) { fs.readFile(path, 'utf-8', (err, data) => { if (err) { - reject(err) + reject(err); } else { - resolve(data) + resolve(data); } - }) - }) + }); + }); } beforeEach(async () => { const moduleFixture = await Test.createTestingModule({ imports: [AppModule], - }).compile() + }).compile(); - app = moduleFixture.createNestApplication() - await app.init() - commandModule = new CommandModuleTest(app.select(CommandModule)) - }) + app = moduleFixture.createNestApplication(); + await app.init(); + commandModule = new CommandModuleTest(app.select(CommandModule)); + }); //=========================================================================================================== it(PARSE_COMMAND_INTEGRATION_TEST_CASE_1.expectation.should, async () => { await commandModule.execute( PARSE_COMMAND_INTEGRATION_TEST_CASE_1.data.comman, PARSE_COMMAND_INTEGRATION_TEST_CASE_1.data.args, - ) - const result = await readFile(FIXTURE_BASE_PATH + '/fixture-output.log') - expect(JSON.parse(result).length).toBeGreaterThan(0) - }) + ); + const result = await readFile(FIXTURE_BASE_PATH + '/fixture-output.log'); + expect(JSON.parse(result).length).toBeGreaterThan(0); + }); //.......................................................................................................... it(PARSE_COMMAND_INTEGRATION_TEST_CASE_2.expectation.should, async () => { await commandModule.execute( - PARSE_COMMAND_INTEGRATION_TEST_CASE_2.data.comman, - PARSE_COMMAND_INTEGRATION_TEST_CASE_2.data.args, - ) - const result = await readFile(FIXTURE_BASE_PATH + '/fixture-output.log') - expect(JSON.parse(result).filter((log: { logLevel: string })=>log.logLevel==PARSE_COMMAND_INTEGRATION_TEST_CASE_2.data.args['log-level']).length).toBeGreaterThan(0) - }) + PARSE_COMMAND_INTEGRATION_TEST_CASE_2.data.comman, + PARSE_COMMAND_INTEGRATION_TEST_CASE_2.data.args, + ); + const result = await readFile(FIXTURE_BASE_PATH + '/fixture-output.log'); + expect( + JSON.parse(result).filter( + (log: { logLevel: string }) => + log.logLevel == + PARSE_COMMAND_INTEGRATION_TEST_CASE_2.data.args['log-level'], + ).length, + ).toBeGreaterThan(0); + }); //.......................................................................................................... it(PARSE_COMMAND_INTEGRATION_TEST_CASE_3.expectation.should, async () => { try { - await commandModule.execute( - PARSE_COMMAND_INTEGRATION_TEST_CASE_3.data.comman, - PARSE_COMMAND_INTEGRATION_TEST_CASE_3.data.args, - ) + await commandModule.execute( + PARSE_COMMAND_INTEGRATION_TEST_CASE_3.data.comman, + PARSE_COMMAND_INTEGRATION_TEST_CASE_3.data.args, + ); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - - }) + }); afterAll(() => { - fs.unlinkSync(FIXTURE_BASE_PATH + '/fixture-output.log') - }) -}) + fs.unlinkSync(FIXTURE_BASE_PATH + '/fixture-output.log'); + }); +}); diff --git a/test/unit-tests/factories/helper.factory.ts b/test/unit-tests/factories/helper.factory.ts index c5d7e61..7e3ec1d 100644 --- a/test/unit-tests/factories/helper.factory.ts +++ b/test/unit-tests/factories/helper.factory.ts @@ -1,85 +1,87 @@ -import { datatype } from "faker" +import { datatype } from 'faker'; export const LOG_PARSER_TEST_CASE_1 = { - data: `2044-08-09T02:12:51.253Z - info - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Service is started"}`, - logLevel:['info'], - expectation: { - should: "should return log object that matches the object in this test case ", - value:{ - timestamp:2354321571253, - logLevel:'info', - transactionId:'9abc55b2-807b-4361-9dbe-aa88b1b2e978', - err:'Service is started' - } - } -} + data: `2044-08-09T02:12:51.253Z - info - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Service is started"}`, + logLevel: ['info'], + expectation: { + should: + 'should return log object that matches the object in this test case ', + value: { + timestamp: 2354321571253, + logLevel: 'info', + transactionId: '9abc55b2-807b-4361-9dbe-aa88b1b2e978', + err: 'Service is started', + }, + }, +}; export const LOG_PARSER_TEST_CASE_2 = { - data: datatype.string(500), - logLevel:['info','error','debug','warn'], - expectation: { - should: "should throw not acceptable exception " - } -} + data: datatype.string(500), + logLevel: ['info', 'error', 'debug', 'warn'], + expectation: { + should: 'should throw not acceptable exception ', + }, +}; export const LOG_PARSER_TEST_CASE_3 = { - data: `2021-08-09T02:12:51.254Z - debug -`, - logLevel:['info','error','debug','warn'], - expectation: { - should: "should throw not acceptable exception" - } -} + data: `2021-08-09T02:12:51.254Z - debug -`, + logLevel: ['info', 'error', 'debug', 'warn'], + expectation: { + should: 'should throw not acceptable exception', + }, +}; export const LOG_PARSER_TEST_CASE_4 = { - data: `2021-08-09T02:12:51. - debug - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"About to request the user information","userId": 10} + data: `2021-08-09T02:12:51. - debug - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"About to request the user information","userId": 10} 2021-08-09T02:12:51.254Z - debug - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"About to request user orders list","userId": 10}`, - logLevel:['info','error','debug','warn'], - expectation: { - should: "should throw not acceptable exception" - } -} + logLevel: ['info', 'error', 'debug', 'warn'], + expectation: { + should: 'should throw not acceptable exception', + }, +}; export const LOG_PARSER_TEST_CASE_5 = { - data: `2044-08-09T02:12:51.253Z - info - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Service is started"}`, - logLevel:[datatype.string(5)], - expectation: { - should: "should return log object that matches the object in this test case ", - value:undefined - } -} + data: `2044-08-09T02:12:51.253Z - info - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Service is started"}`, + logLevel: [datatype.string(5)], + expectation: { + should: + 'should return log object that matches the object in this test case ', + value: undefined, + }, +}; export const LOG_PARSER_TEST_CASE_6 = { - data: `2021-08-09T02:12:51.262Z - debug - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e821","details":"User information is retrieved","user":{"id":16,"orders":[{"id":472,"items":{"id":7,"price":7.12}}]}}`, - logLevel:['debug'], - expectation: { - should: "should contain a correct log level ", - value:{ - logLevel:'debug' - } - } -} + data: `2021-08-09T02:12:51.262Z - debug - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e821","details":"User information is retrieved","user":{"id":16,"orders":[{"id":472,"items":{"id":7,"price":7.12}}]}}`, + logLevel: ['debug'], + expectation: { + should: 'should contain a correct log level ', + value: { + logLevel: 'debug', + }, + }, +}; export const LOG_PARSER_TEST_CASE_7 = { - data: `2021-08-09T02:12:51.259Z - error - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Cannot find user orders list","code": 404,"err":"Not found"}`, - logLevel:['error'], - expectation: { - should: "should contain a correct log level ", - value:{ - logLevel:'error', - transactionId:'9abc55b2-807b-4361-9dbe-aa88b1b2e978' - } - } -} + data: `2021-08-09T02:12:51.259Z - error - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Cannot find user orders list","code": 404,"err":"Not found"}`, + logLevel: ['error'], + expectation: { + should: 'should contain a correct log level ', + value: { + logLevel: 'error', + transactionId: '9abc55b2-807b-4361-9dbe-aa88b1b2e978', + }, + }, +}; export const LOG_PARSER_TEST_CASE_8 = { - data: `2021-08-09T02:12:51.264Z - warn - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Service finished with error","code":404,"err":"Cannot find user orders list"}`, - logLevel:['error','warn'], - expectation: { - should: "should contain a correct log level ", - value:{ - logLevel:'warn', - transactionId:'9abc55b2-807b-4361-9dbe-aa88b1b2e978', - err:'Service finished with error' - } - } -} \ No newline at end of file + data: `2021-08-09T02:12:51.264Z - warn - {"transactionId":"9abc55b2-807b-4361-9dbe-aa88b1b2e978","details":"Service finished with error","code":404,"err":"Cannot find user orders list"}`, + logLevel: ['error', 'warn'], + expectation: { + should: 'should contain a correct log level ', + value: { + logLevel: 'warn', + transactionId: '9abc55b2-807b-4361-9dbe-aa88b1b2e978', + err: 'Service finished with error', + }, + }, +}; diff --git a/test/unit-tests/factories/index.ts b/test/unit-tests/factories/index.ts index 62ea650..ff0df8a 100644 --- a/test/unit-tests/factories/index.ts +++ b/test/unit-tests/factories/index.ts @@ -1,3 +1,3 @@ -export * from './parser.factory' -export * from './helper.factory' -export * from './utils.factory' \ No newline at end of file +export * from './parser.factory'; +export * from './helper.factory'; +export * from './utils.factory'; diff --git a/test/unit-tests/factories/parser.factory.ts b/test/unit-tests/factories/parser.factory.ts index 01fd4b4..bdc7d58 100644 --- a/test/unit-tests/factories/parser.factory.ts +++ b/test/unit-tests/factories/parser.factory.ts @@ -1,92 +1,94 @@ -import * as path from 'path' -import faker, { datatype } from 'faker'; +import { datatype } from 'faker'; +import * as path from 'path'; export const FIXTURE_BASE_PATH = path.resolve(__dirname, '../fixtures'); //===================================================================================== export const PARSER_INPUT_TEST_CASE_1 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: ['error'] - }, - expectation: { - should: "should return at least one log " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: ['error'], + }, + expectation: { + should: 'should return at least one log ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_2 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: ['error','debug'] - }, - expectation: { - should: "should return a list of logs and has debug and error inside " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: ['error', 'debug'], + }, + expectation: { + should: 'should return a list of logs and has debug and error inside ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_3 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: ['unknown'] - }, - expectation: { - should: "should return a list with 0 elements inside " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: ['unknown'], + }, + expectation: { + should: 'should return a list with 0 elements inside ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_4 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: ['error','warn','debug'] - }, - expectation: { - should: "should return a list of logs and has debug, warn and error inside " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: ['error', 'warn', 'debug'], + }, + expectation: { + should: + 'should return a list of logs and has debug, warn and error inside ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_5 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: ['error','warn','info','debug'] - }, - expectation: { - should: "should return a list of logs and has debug, wanr, info and error inside " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: ['error', 'warn', 'info', 'debug'], + }, + expectation: { + should: + 'should return a list of logs and has debug, wanr, info and error inside ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_6 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: [datatype.string(20)] - }, - expectation: { - should: "should return a list with 0 elements inside " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: [datatype.string(20)], + }, + expectation: { + should: 'should return a list with 0 elements inside ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_7 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: [datatype.number(345464)] - }, - expectation: { - should: "should throw error " - } -} + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: [datatype.number(345464)], + }, + expectation: { + should: 'should throw error ', + }, +}; //===================================================================================== export const PARSER_INPUT_TEST_CASE_8 = { - data: { - input: FIXTURE_BASE_PATH + '/fixture-input.log', - output: FIXTURE_BASE_PATH + '/fixture-output.log', - logLevel: [datatype.string(20),datatype.string(20),datatype.string(20)] - }, - expectation: { - should: "should return a list with 0 elements inside" - } -} \ No newline at end of file + data: { + input: FIXTURE_BASE_PATH + '/fixture-input.log', + output: FIXTURE_BASE_PATH + '/fixture-output.log', + logLevel: [datatype.string(20), datatype.string(20), datatype.string(20)], + }, + expectation: { + should: 'should return a list with 0 elements inside', + }, +}; diff --git a/test/unit-tests/factories/utils.factory.ts b/test/unit-tests/factories/utils.factory.ts index 1124d6a..3c8eb58 100644 --- a/test/unit-tests/factories/utils.factory.ts +++ b/test/unit-tests/factories/utils.factory.ts @@ -1,151 +1,148 @@ -import { datatype } from "faker" +import { datatype } from 'faker'; -export const DTO_VALIDATION_TEST_CASE_1={ - data:{ - input:'input.log', - output:'output.log', - logLevel:['error','warn','info','debug'] - }, - expectation:{ - should:'should return ok' - } -} +export const DTO_VALIDATION_TEST_CASE_1 = { + data: { + input: 'input.log', + output: 'output.log', + logLevel: ['error', 'warn', 'info', 'debug'], + }, + expectation: { + should: 'should return ok', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_2={ - data:{ - input:'input.log', - output:'output.log', - logLevel:[] - }, - expectation:{ - should:'should return ok' - } -} +export const DTO_VALIDATION_TEST_CASE_2 = { + data: { + input: 'input.log', + output: 'output.log', + logLevel: [], + }, + expectation: { + should: 'should return ok', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_3={ - data:{ - input:datatype.string(5), - output:'output.log', - logLevel:['error'] - }, - expectation:{ - should:'should return ok' - } -} +export const DTO_VALIDATION_TEST_CASE_3 = { + data: { + input: datatype.string(5), + output: 'output.log', + logLevel: ['error'], + }, + expectation: { + should: 'should return ok', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_4={ - data:{ - input:datatype.string(5), - output:datatype.string(5), - logLevel:[] - }, - expectation:{ - should:'should return ok' - } -} +export const DTO_VALIDATION_TEST_CASE_4 = { + data: { + input: datatype.string(5), + output: datatype.string(5), + logLevel: [], + }, + expectation: { + should: 'should return ok', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_5={ - data:{ - input:datatype.number(5), - output:datatype.number(5), - logLevel:[] - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_5 = { + data: { + input: datatype.number(5), + output: datatype.number(5), + logLevel: [], + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_6={ - data:{ - input:'input.log', - output:'output.log', - logLevel:[datatype.string(10)] - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_6 = { + data: { + input: 'input.log', + output: 'output.log', + logLevel: [datatype.string(10)], + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_7={ - data:{ - input:'input.log', - output:'output.log', - logLevel:[datatype.number(200)] - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_7 = { + data: { + input: 'input.log', + output: 'output.log', + logLevel: [datatype.number(200)], + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_8={ - data:{ - input:'input.log', - output:'output.log', - logLevel:datatype.number(200) - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_8 = { + data: { + input: 'input.log', + output: 'output.log', + logLevel: datatype.number(200), + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_9={ - data:{ - input:'input.log', - logLevel:datatype.number(200) - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_9 = { + data: { + input: 'input.log', + logLevel: datatype.number(200), + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_10={ - data:{ - output:'output.log', - logLevel:datatype.number(200) - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_10 = { + data: { + output: 'output.log', + logLevel: datatype.number(200), + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_11={ - data:{ - logLevel:datatype.number(200) - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_11 = { + data: { + logLevel: datatype.number(200), + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_12={ - data:{ - input:'input.log', - output:'output.log', - - }, - expectation:{ - should:'should return ok' - } -} +export const DTO_VALIDATION_TEST_CASE_12 = { + data: { + input: 'input.log', + output: 'output.log', + }, + expectation: { + should: 'should return ok', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_13={ - data:{ - input:true, - output:'output.log', - - }, - expectation:{ - should:'should return error' - } -} +export const DTO_VALIDATION_TEST_CASE_13 = { + data: { + input: true, + output: 'output.log', + }, + expectation: { + should: 'should return error', + }, +}; //=========================================================================================== -export const DTO_VALIDATION_TEST_CASE_14={ - data:{ - input:'input.log', - output:false, - - }, - expectation:{ - should:'should return error' - } -} \ No newline at end of file +export const DTO_VALIDATION_TEST_CASE_14 = { + data: { + input: 'input.log', + output: false, + }, + expectation: { + should: 'should return error', + }, +}; diff --git a/test/unit-tests/filehelper.spec.ts b/test/unit-tests/filehelper.spec.ts index 0e224e9..1332587 100644 --- a/test/unit-tests/filehelper.spec.ts +++ b/test/unit-tests/filehelper.spec.ts @@ -1,67 +1,114 @@ -import { FileHelper } from "../../src/parser/helper"; -import { LOG_PARSER_TEST_CASE_1, LOG_PARSER_TEST_CASE_2, LOG_PARSER_TEST_CASE_3, LOG_PARSER_TEST_CASE_4, LOG_PARSER_TEST_CASE_5, LOG_PARSER_TEST_CASE_6, LOG_PARSER_TEST_CASE_7, LOG_PARSER_TEST_CASE_8 } from './factories' - -describe("tests the behavior of file helper class",()=>{ - - let fileHelper = new FileHelper(); - //================================================================================================================== - it(LOG_PARSER_TEST_CASE_1.expectation.should,async()=>{ - const result=await fileHelper.parsLog(LOG_PARSER_TEST_CASE_1.data,LOG_PARSER_TEST_CASE_1.logLevel); - expect(result.timestamp).toEqual(LOG_PARSER_TEST_CASE_1.expectation.value.timestamp); - expect(result.logLevel).toEqual(LOG_PARSER_TEST_CASE_1.expectation.value.logLevel); - expect(result.transactionId).toEqual(LOG_PARSER_TEST_CASE_1.expectation.value.transactionId); - expect(result.err).toEqual(LOG_PARSER_TEST_CASE_1.expectation.value.err); - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_2.expectation.should,async()=>{ - try { - await fileHelper.parsLog(LOG_PARSER_TEST_CASE_2.data,LOG_PARSER_TEST_CASE_2.logLevel); - } catch (error) { - expect(error).toBeDefined(); - } - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_3.expectation.should,async()=>{ - try { - await fileHelper.parsLog(LOG_PARSER_TEST_CASE_3.data,LOG_PARSER_TEST_CASE_3.logLevel); - } catch (error) { - expect(error).toBeDefined(); - } - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_4.expectation.should,async()=>{ - try { - await fileHelper.parsLog(LOG_PARSER_TEST_CASE_4.data,LOG_PARSER_TEST_CASE_4.logLevel); - } catch (error) { - expect(error).toBeDefined(); - } - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_5.expectation.should,async()=>{ - const result=await fileHelper.parsLog(LOG_PARSER_TEST_CASE_5.data,LOG_PARSER_TEST_CASE_5.logLevel); - expect(result).toBeUndefined(); - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_6.expectation.should,async()=>{ - const result=await fileHelper.parsLog(LOG_PARSER_TEST_CASE_6.data,LOG_PARSER_TEST_CASE_6.logLevel); - expect(result.logLevel).toEqual(LOG_PARSER_TEST_CASE_6.expectation.value.logLevel); - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_7.expectation.should,async()=>{ - const result=await fileHelper.parsLog(LOG_PARSER_TEST_CASE_7.data,LOG_PARSER_TEST_CASE_7.logLevel); - expect(result.logLevel).toEqual(LOG_PARSER_TEST_CASE_7.expectation.value.logLevel); - expect(result.transactionId).toEqual(LOG_PARSER_TEST_CASE_7.expectation.value.transactionId); - }) - //.................................................................................................................. - it(LOG_PARSER_TEST_CASE_8.expectation.should,async()=>{ - const result=await fileHelper.parsLog(LOG_PARSER_TEST_CASE_8.data,LOG_PARSER_TEST_CASE_8.logLevel); - expect(result.logLevel).toEqual(LOG_PARSER_TEST_CASE_8.expectation.value.logLevel); - expect(result.transactionId).toEqual(LOG_PARSER_TEST_CASE_8.expectation.value.transactionId); - expect(result.err).toEqual(LOG_PARSER_TEST_CASE_8.expectation.value.err); - }) - //.................................................................................................................. - //.................................................................................................................. - //.................................................................................................................. - //.................................................................................................................. -}) +import { FileHelper } from '../../src/parser/helper'; +import { + LOG_PARSER_TEST_CASE_1, + LOG_PARSER_TEST_CASE_2, + LOG_PARSER_TEST_CASE_3, + LOG_PARSER_TEST_CASE_4, + LOG_PARSER_TEST_CASE_5, + LOG_PARSER_TEST_CASE_6, + LOG_PARSER_TEST_CASE_7, + LOG_PARSER_TEST_CASE_8, +} from './factories'; +describe('tests the behavior of file helper class', () => { + const fileHelper = new FileHelper(); + //================================================================================================================== + it(LOG_PARSER_TEST_CASE_1.expectation.should, async () => { + const result = await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_1.data, + LOG_PARSER_TEST_CASE_1.logLevel, + ); + expect(result.timestamp).toEqual( + LOG_PARSER_TEST_CASE_1.expectation.value.timestamp, + ); + expect(result.logLevel).toEqual( + LOG_PARSER_TEST_CASE_1.expectation.value.logLevel, + ); + expect(result.transactionId).toEqual( + LOG_PARSER_TEST_CASE_1.expectation.value.transactionId, + ); + expect(result.err).toEqual(LOG_PARSER_TEST_CASE_1.expectation.value.err); + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_2.expectation.should, async () => { + try { + await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_2.data, + LOG_PARSER_TEST_CASE_2.logLevel, + ); + } catch (error) { + expect(error).toBeDefined(); + } + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_3.expectation.should, async () => { + try { + await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_3.data, + LOG_PARSER_TEST_CASE_3.logLevel, + ); + } catch (error) { + expect(error).toBeDefined(); + } + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_4.expectation.should, async () => { + try { + await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_4.data, + LOG_PARSER_TEST_CASE_4.logLevel, + ); + } catch (error) { + expect(error).toBeDefined(); + } + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_5.expectation.should, async () => { + const result = await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_5.data, + LOG_PARSER_TEST_CASE_5.logLevel, + ); + expect(result).toBeUndefined(); + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_6.expectation.should, async () => { + const result = await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_6.data, + LOG_PARSER_TEST_CASE_6.logLevel, + ); + expect(result.logLevel).toEqual( + LOG_PARSER_TEST_CASE_6.expectation.value.logLevel, + ); + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_7.expectation.should, async () => { + const result = await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_7.data, + LOG_PARSER_TEST_CASE_7.logLevel, + ); + expect(result.logLevel).toEqual( + LOG_PARSER_TEST_CASE_7.expectation.value.logLevel, + ); + expect(result.transactionId).toEqual( + LOG_PARSER_TEST_CASE_7.expectation.value.transactionId, + ); + }); + //.................................................................................................................. + it(LOG_PARSER_TEST_CASE_8.expectation.should, async () => { + const result = await fileHelper.parsLog( + LOG_PARSER_TEST_CASE_8.data, + LOG_PARSER_TEST_CASE_8.logLevel, + ); + expect(result.logLevel).toEqual( + LOG_PARSER_TEST_CASE_8.expectation.value.logLevel, + ); + expect(result.transactionId).toEqual( + LOG_PARSER_TEST_CASE_8.expectation.value.transactionId, + ); + expect(result.err).toEqual(LOG_PARSER_TEST_CASE_8.expectation.value.err); + }); + //.................................................................................................................. + //.................................................................................................................. + //.................................................................................................................. + //.................................................................................................................. +}); diff --git a/test/unit-tests/parser.service.spec.ts b/test/unit-tests/parser.service.spec.ts index 9822bc0..d333d7c 100644 --- a/test/unit-tests/parser.service.spec.ts +++ b/test/unit-tests/parser.service.spec.ts @@ -1,82 +1,196 @@ import * as fs from 'fs'; -import { ParserCommandDTO } from "../../src/parser/dtos/parser-command-dto"; -import { FileHelper } from "../../src/parser/helper"; -import { ParserService } from "../../src/parser/service/parser.service"; +import { ParserCommandDTO } from '../../src/parser/dtos/parser-command-dto'; +import { FileHelper } from '../../src/parser/helper'; +import { ParserService } from '../../src/parser/service/parser.service'; import { - FIXTURE_BASE_PATH, - PARSER_INPUT_TEST_CASE_1, - PARSER_INPUT_TEST_CASE_2, - PARSER_INPUT_TEST_CASE_3, - PARSER_INPUT_TEST_CASE_4, - PARSER_INPUT_TEST_CASE_5, - PARSER_INPUT_TEST_CASE_6, - PARSER_INPUT_TEST_CASE_7, - PARSER_INPUT_TEST_CASE_8 -} from './factories' + FIXTURE_BASE_PATH, + PARSER_INPUT_TEST_CASE_1, + PARSER_INPUT_TEST_CASE_2, + PARSER_INPUT_TEST_CASE_3, + PARSER_INPUT_TEST_CASE_4, + PARSER_INPUT_TEST_CASE_5, + PARSER_INPUT_TEST_CASE_6, + PARSER_INPUT_TEST_CASE_7, + PARSER_INPUT_TEST_CASE_8, +} from './factories'; - -describe("tests the behavior of parser service class", () => { - let fileHelper = new FileHelper(); - let service = new ParserService(fileHelper); - //================================================================================================================== - it(PARSER_INPUT_TEST_CASE_1.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_1.data)); - expect(result.length).toBeGreaterThan(0); - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_2.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_2.data)); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'debug')).length).toBeGreaterThan(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'error')).length).toBeGreaterThan(0); - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_3.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_3.data)); - expect(await result.length).toEqual(0); - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_4.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_4.data)); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'warn')).length).toBeGreaterThan(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'error')).length).toBeGreaterThan(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'debug')).length).toBeGreaterThan(0); - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_5.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_5.data)); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'warn')).length).toBeGreaterThan(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'error')).length).toBeGreaterThan(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'debug')).length).toBeGreaterThan(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'info')).length).toBeGreaterThan(0); - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_6.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_6.data)); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'warn')).length).toEqual(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'error')).length).toEqual(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'debug')).length).toEqual(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'info')).length).toEqual(0); - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_7.expectation.should, async () => { - try { - await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_7.data)); - } catch (error) { - expect(error).toBeDefined(); - } - }) - //.................................................................................................................. - it(PARSER_INPUT_TEST_CASE_8.expectation.should, async () => { - const result = await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_8.data)); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'warn')).length).toEqual(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'error')).length).toEqual(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'debug')).length).toEqual(0); - expect((await result.filter((log: { logLevel: string; }) => log.logLevel === 'info')).length).toEqual(0); - }) - //.................................................................................................................. - //================================================================================================================== - afterAll(() => { - fs.unlinkSync(FIXTURE_BASE_PATH + '/fixture-output.log'); - }) - -}) \ No newline at end of file +describe('tests the behavior of parser service class', () => { + const fileHelper = new FileHelper(); + const service = new ParserService(fileHelper); + //================================================================================================================== + it(PARSER_INPUT_TEST_CASE_1.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_1.data), + ); + expect(result.length).toBeGreaterThan(0); + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_2.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_2.data), + ); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'debug', + ) + ).length, + ).toBeGreaterThan(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'error', + ) + ).length, + ).toBeGreaterThan(0); + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_3.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_3.data), + ); + expect(await result.length).toEqual(0); + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_4.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_4.data), + ); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'warn', + ) + ).length, + ).toBeGreaterThan(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'error', + ) + ).length, + ).toBeGreaterThan(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'debug', + ) + ).length, + ).toBeGreaterThan(0); + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_5.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_5.data), + ); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'warn', + ) + ).length, + ).toBeGreaterThan(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'error', + ) + ).length, + ).toBeGreaterThan(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'debug', + ) + ).length, + ).toBeGreaterThan(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'info', + ) + ).length, + ).toBeGreaterThan(0); + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_6.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_6.data), + ); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'warn', + ) + ).length, + ).toEqual(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'error', + ) + ).length, + ).toEqual(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'debug', + ) + ).length, + ).toEqual(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'info', + ) + ).length, + ).toEqual(0); + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_7.expectation.should, async () => { + try { + await service.parse(new ParserCommandDTO(PARSER_INPUT_TEST_CASE_7.data)); + } catch (error) { + expect(error).toBeDefined(); + } + }); + //.................................................................................................................. + it(PARSER_INPUT_TEST_CASE_8.expectation.should, async () => { + const result = await service.parse( + new ParserCommandDTO(PARSER_INPUT_TEST_CASE_8.data), + ); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'warn', + ) + ).length, + ).toEqual(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'error', + ) + ).length, + ).toEqual(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'debug', + ) + ).length, + ).toEqual(0); + expect( + ( + await result.filter( + (log: { logLevel: string }) => log.logLevel === 'info', + ) + ).length, + ).toEqual(0); + }); + //.................................................................................................................. + //================================================================================================================== + afterAll(() => { + fs.unlinkSync(FIXTURE_BASE_PATH + '/fixture-output.log'); + }); +}); diff --git a/test/unit-tests/utils.spec.ts b/test/unit-tests/utils.spec.ts index d693764..8eed23a 100644 --- a/test/unit-tests/utils.spec.ts +++ b/test/unit-tests/utils.spec.ts @@ -1,5 +1,5 @@ -import { validateDTO } from '../../src/parser/common' -import { ParserCommandDTO } from '../../src/parser/dtos/parser-command-dto' +import { validateDTO } from '../../src/parser/common'; +import { ParserCommandDTO } from '../../src/parser/dtos/parser-command-dto'; import { DTO_VALIDATION_TEST_CASE_1, DTO_VALIDATION_TEST_CASE_10, @@ -15,119 +15,119 @@ import { DTO_VALIDATION_TEST_CASE_7, DTO_VALIDATION_TEST_CASE_8, DTO_VALIDATION_TEST_CASE_9, -} from './factories' +} from './factories'; describe('tests the functions inside utils file', () => { //================================================================================================================== it(DTO_VALIDATION_TEST_CASE_1.expectation.should, async () => { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_1.data) - await validateDTO(dto) - expect(dto.input).toBeDefined() - }) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_1.data); + await validateDTO(dto); + expect(dto.input).toBeDefined(); + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_2.expectation.should, async () => { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_2.data) - await validateDTO(dto) - expect(dto.input).toBeDefined() - }) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_2.data); + await validateDTO(dto); + expect(dto.input).toBeDefined(); + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_3.expectation.should, async () => { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_3.data) - await validateDTO(dto) - expect(dto.input).toBeDefined() - }) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_3.data); + await validateDTO(dto); + expect(dto.input).toBeDefined(); + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_4.expectation.should, async () => { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_4.data) - await validateDTO(dto) - expect(dto.input).toBeDefined() - }) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_4.data); + await validateDTO(dto); + expect(dto.input).toBeDefined(); + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_5.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_5.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_5.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_6.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_6.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_6.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_7.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_7.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_7.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_8.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_8.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_8.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_9.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_9.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_9.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_10.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_10.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_10.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_11.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_11.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_11.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_12.expectation.should, async () => { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_12.data) - await validateDTO(dto) - expect(dto.input).toBeDefined() - }) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_12.data); + await validateDTO(dto); + expect(dto.input).toBeDefined(); + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_13.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_13.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_13.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. it(DTO_VALIDATION_TEST_CASE_14.expectation.should, async () => { try { - const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_14.data) - await validateDTO(dto) + const dto = new ParserCommandDTO(DTO_VALIDATION_TEST_CASE_14.data); + await validateDTO(dto); } catch (error) { - expect(error).toBeDefined() + expect(error).toBeDefined(); } - }) + }); //.................................................................................................................. -}) +}); From f6989db03a8e46893bea925cbbafc3f56cadb628 Mon Sep 17 00:00:00 2001 From: moeidheidari Date: Thu, 23 Jun 2022 02:20:50 +0300 Subject: [PATCH 2/2] github action files added --- .github/workflows/app.yaml | 26 ++++++++++++++++++++++++++ log.json | 8 ++++++++ 2 files changed, 34 insertions(+) create mode 100755 .github/workflows/app.yaml create mode 100644 log.json diff --git a/.github/workflows/app.yaml b/.github/workflows/app.yaml new file mode 100755 index 0000000..14158ed --- /dev/null +++ b/.github/workflows/app.yaml @@ -0,0 +1,26 @@ +name: Node.js CI + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [10.x, 12.x, 14.x, 15.x] + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - run: npm ci + - run: npm run build --if-present + - run: npm test diff --git a/log.json b/log.json new file mode 100644 index 0000000..0b8cad7 --- /dev/null +++ b/log.json @@ -0,0 +1,8 @@ +[ + { + "timestamp": 1628475171259, + "logLevel": "error", + "transactionId": "9abc55b2-807b-4361-9dbe-aa88b1b2e978", + "err": "Cannot find user orders list" + } +] \ No newline at end of file