Skip to content

Commit

Permalink
modify the solution to not catch the client's exception in the use ca…
Browse files Browse the repository at this point in the history
…se, but in the console app's command
  • Loading branch information
bn-pass committed Nov 22, 2023
1 parent 046589f commit f269a64
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 54 deletions.
1 change: 1 addition & 0 deletions apps/server/src/modules/deletion/console/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './push-delete-requests-options.builder';
export * from './trigger-deletion-execution-options.builder';
export * from './deletion-execution-trigger-result.builder';
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Test, TestingModule } from '@nestjs/testing';
import { createMock } from '@golevelup/ts-jest';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ConsoleWriterService } from '@infra/console';
import { DeletionExecutionUc } from '../uc';
import { DeletionExecutionConsole } from './deletion-execution.console';
import { TriggerDeletionExecutionOptionsBuilder } from './builder';
import { DeletionExecutionTriggerResultBuilder, TriggerDeletionExecutionOptionsBuilder } from './builder';

describe(DeletionExecutionConsole.name, () => {
let module: TestingModule;
let console: DeletionExecutionConsole;
let deletionExecutionUc: DeletionExecutionUc;
let deletionExecutionUc: DeepMocked<DeletionExecutionUc>;

beforeAll(async () => {
module = await Test.createTestingModule({
Expand Down Expand Up @@ -61,5 +61,49 @@ describe(DeletionExecutionConsole.name, () => {
expect(spy).toBeCalledWith(limit);
});
});

describe(`when ${DeletionExecutionUc.name}'s triggerDeletionExecution() method doesn't throw an exception`, () => {
const setup = () => {
const options = TriggerDeletionExecutionOptionsBuilder.build(1000);

deletionExecutionUc.triggerDeletionExecution.mockResolvedValueOnce(undefined);

const spy = jest.spyOn(DeletionExecutionTriggerResultBuilder, 'buildSuccess');

return { options, spy };
};

it('should prepare result indicating success', async () => {
const { options, spy } = setup();

await console.triggerDeletionExecution(options);

expect(spy).toHaveBeenCalled();
});
});

describe(`when ${DeletionExecutionUc.name}'s triggerDeletionExecution() method throws an exception`, () => {
const setup = () => {
const options = TriggerDeletionExecutionOptionsBuilder.build(1000);

const err = new Error('some error occurred...');

deletionExecutionUc.triggerDeletionExecution.mockRejectedValueOnce(err);

// const spy = jest.spyOn(ErrorMapper, 'mapRpcErrorResponseToDomainError');

const spy = jest.spyOn(DeletionExecutionTriggerResultBuilder, 'buildFailure');

return { options, err, spy };
};

it('should prepare result indicating failure', async () => {
const { options, err, spy } = setup();

await console.triggerDeletionExecution(options);

expect(spy).toHaveBeenCalledWith(err);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Command, Console } from 'nestjs-console';
import { ConsoleWriterService } from '@infra/console';
import { DeletionExecutionUc } from '../uc';
import { TriggerDeletionExecutionOptions } from './interface';
import { DeletionExecutionTriggerResultBuilder } from './builder';
import { DeletionExecutionTriggerResult, TriggerDeletionExecutionOptions } from './interface';

@Console({ command: 'execution', description: 'Console providing an access to the deletion execution(s).' })
export class DeletionExecutionConsole {
Expand All @@ -19,9 +20,18 @@ export class DeletionExecutionConsole {
],
})
async triggerDeletionExecution(options: TriggerDeletionExecutionOptions): Promise<void> {
const result = await this.deletionExecutionUc.triggerDeletionExecution(
options.limit ? Number(options.limit) : undefined
);
// Try to trigger the deletion execution(s) via Deletion API client,
// return successful status in case of a success, otherwise return
// a result with a failure status and a proper error message.
let result: DeletionExecutionTriggerResult;

try {
await this.deletionExecutionUc.triggerDeletionExecution(options.limit ? Number(options.limit) : undefined);

result = DeletionExecutionTriggerResultBuilder.buildSuccess();
} catch (err) {
result = DeletionExecutionTriggerResultBuilder.buildFailure(err as Error);
}

this.consoleWriter.info(JSON.stringify(result));
}
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/modules/deletion/console/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './push-delete-requests-options.interface';
export * from './trigger-deletion-execution-options.interface';
export * from './deletion-execution-trigger-status.enum';
export * from './deletion-execution-trigger-result';
1 change: 0 additions & 1 deletion apps/server/src/modules/deletion/uc/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './batch-deletion-summary-detail.builder';
export * from './batch-deletion-summary.builder';
export * from './deletion-execution-trigger-result.builder';
49 changes: 18 additions & 31 deletions apps/server/src/modules/deletion/uc/deletion-execution.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { DeletionClient } from '../client';
import { DeletionExecutionUc } from './deletion-execution.uc';
import { DeletionExecutionTriggerResultBuilder } from './builder';

describe(DeletionExecutionUc.name, () => {
let module: TestingModule;
Expand Down Expand Up @@ -37,45 +36,33 @@ describe(DeletionExecutionUc.name, () => {
});

describe('triggerDeletionExecution', () => {
describe('when called with valid input', () => {
describe("when client doesn't throw any error", () => {
const setup = () => {
const limit = 1000;
describe("when client doesn't throw any error", () => {
const setup = () => {
const limit = 1000;

deletionClient.executeDeletions.mockResolvedValueOnce(undefined);
deletionClient.executeDeletions.mockResolvedValueOnce(undefined);

const expectedOutput = DeletionExecutionTriggerResultBuilder.buildSuccess();
return { limit };
};

return { limit, expectedOutput };
};
it('should also not throw an error', async () => {
const { limit } = setup();

it('should return an object with the result containing a successful status info', async () => {
const { limit, expectedOutput } = setup();

const output = await uc.triggerDeletionExecution(limit);

expect(output).toStrictEqual(expectedOutput);
});
await expect(uc.triggerDeletionExecution(limit)).resolves.not.toThrow();
});
});

describe('when client throws an error', () => {
const setup = () => {
const error = new Error('connection error');

deletionClient.executeDeletions.mockRejectedValueOnce(error);

const expectedOutput = DeletionExecutionTriggerResultBuilder.buildFailure(error);

return { expectedOutput };
};
describe('when client throws an error', () => {
const setup = () => {
const error = new Error('connection error');

it('should return an object with the result containing a failure status info with proper error message', async () => {
const { expectedOutput } = setup();
deletionClient.executeDeletions.mockRejectedValueOnce(error);
};

const output = await uc.triggerDeletionExecution();
it('should also throw an error', async () => {
setup();

expect(output).toStrictEqual(expectedOutput);
});
await expect(uc.triggerDeletionExecution()).rejects.toThrow();
});
});
});
Expand Down
15 changes: 2 additions & 13 deletions apps/server/src/modules/deletion/uc/deletion-execution.uc.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { Injectable } from '@nestjs/common';
import { DeletionClient } from '../client';
import { DeletionExecutionTriggerResult } from './interface';
import { DeletionExecutionTriggerResultBuilder } from './builder';

@Injectable()
export class DeletionExecutionUc {
constructor(private readonly deletionClient: DeletionClient) {}

async triggerDeletionExecution(limit?: number): Promise<DeletionExecutionTriggerResult> {
// Try to trigger the deletion execution(s) via Deletion API client,
// return successful status in case of a success, otherwise return
// a result with a failure status and a proper error message.
try {
await this.deletionClient.executeDeletions(limit);

return DeletionExecutionTriggerResultBuilder.buildSuccess();
} catch (err) {
return DeletionExecutionTriggerResultBuilder.buildFailure(err as Error);
}
async triggerDeletionExecution(limit?: number): Promise<void> {
await this.deletionClient.executeDeletions(limit);
}
}
2 changes: 0 additions & 2 deletions apps/server/src/modules/deletion/uc/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export * from './batch-deletion-summary-overall-status.enum';
export * from './batch-deletion-summary-detail.interface';
export * from './batch-deletion-summary.interface';
export * from './deletion-execution-trigger-status.enum';
export * from './deletion-execution-trigger-result';
export * from './interfaces';

0 comments on commit f269a64

Please sign in to comment.