Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming covention alignemnt #27

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/apps/human-app/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { JobAssignmentController } from './modules/job-assignment/job-assignment
import { JobAssignmentModule } from './modules/job-assignment/job-assignment.module';
import { StatisticsModule } from './modules/statistics/statistics.module';
import { StatisticsController } from './modules/statistics/statistics.controller';
import { ExternalApiModule } from './integrations/external-api/external-api.module';
import { ExchangeOracleModule } from './integrations/exchange-oracle/exchange-oracle.module';

@Module({
imports: [
Expand All @@ -40,7 +40,7 @@ import { ExternalApiModule } from './integrations/external-api/external-api.modu
JobsDiscoveryModule,
JobAssignmentModule,
ReputationOracleModule,
ExternalApiModule,
ExchangeOracleModule,
CommonConfigModule,
OracleDiscoveryModule,
StatisticsModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { HttpException, HttpStatus } from '@nestjs/common';
import { GlobalExceptionsFilter } from './global-exceptions.filter';
import { Test, TestingModule } from '@nestjs/testing';

describe('GlobalExceptionsFilter', () => {
let filter: GlobalExceptionsFilter;
let mockJson: jest.Mock;
let mockStatus: jest.Mock;
let mockGetResponse: jest.Mock;
let mockHttpArgumentsHost: jest.Mock;
let mockArgumentsHost: any;

beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
providers: [GlobalExceptionsFilter],
}).compile();
filter = module.get<GlobalExceptionsFilter>(GlobalExceptionsFilter);
mockJson = jest.fn();
mockStatus = jest.fn().mockImplementation(() => ({
json: mockJson,
}));
mockGetResponse = jest.fn().mockImplementation(() => ({
status: mockStatus,
}));
mockHttpArgumentsHost = jest.fn().mockImplementation(() => ({
getResponse: mockGetResponse,
getRequest: jest.fn(),
}));

mockArgumentsHost = {
switchToHttp: mockHttpArgumentsHost,
getArgByIndex: jest.fn(),
getArgs: jest.fn(),
getType: jest.fn(),
switchToRpc: jest.fn(),
switchToWs: jest.fn(),
};
});
it('should be defined', () => {
expect(filter).toBeDefined();
});

it('should handle HttpException', () => {
filter.catch(
new HttpException('Http exception', HttpStatus.BAD_REQUEST),
mockArgumentsHost,
);
expect(mockHttpArgumentsHost).toBeCalledTimes(1);
expect(mockHttpArgumentsHost).toBeCalledWith();
expect(mockGetResponse).toBeCalledTimes(1);
expect(mockGetResponse).toBeCalledWith();
expect(mockStatus).toBeCalledTimes(1);
expect(mockStatus).toBeCalledWith(HttpStatus.BAD_REQUEST);
expect(mockJson).toBeCalledTimes(1);
expect(mockJson).toBeCalledWith('Http exception');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';

@Injectable()
export class ExternalApiGateway {
export class ExchangeOracleGateway {
constructor(
private httpService: HttpService,
@InjectMapper() private mapper: Mapper,
Expand All @@ -46,7 +46,7 @@ export class ExternalApiGateway {
): Promise<UserStatisticsResponse> {
const options: AxiosRequestConfig = {
method: 'GET',
url: `${command.oracleUrl}/stats/assignment`,
url: `${command.exchangeOracleUrl}/stats/assignment`,
headers: {
Authorization: `Bearer ${command.token}`,
},
Expand All @@ -58,7 +58,7 @@ export class ExternalApiGateway {
): Promise<OracleStatisticsResponse> {
const options: AxiosRequestConfig = {
method: 'GET',
url: `${command.oracleUrl}/stats`,
url: `${command.exchangeOracleUrl}/stats`,
};
return this.callExternalHttpUtilRequest<OracleStatisticsResponse>(options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '../../modules/jobs-discovery/interfaces/jobs-discovery.interface';

@Injectable()
export class ExternalApiProfile extends AutomapperProfile {
export class ExchangeOracleProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { ExchangeOracleGateway } from './exchange-oracle.gateway';
import { ExchangeOracleProfile } from './exchange-oracle.mapper';

@Module({
imports: [HttpModule],
providers: [ExchangeOracleGateway, ExchangeOracleProfile],
exports: [ExchangeOracleGateway],
})
export class ExchangeOracleModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const exchangeOracleGatewayMock = {
fetchOracleStatistics: jest.fn(),
fetchUserStatistics: jest.fn(),
fetchAssignedJobs: jest.fn(),
postNewJobAssignment: jest.fn(),
fetchDiscoveredJobs: jest.fn(),
};
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HttpService } from '@nestjs/axios';
import { ExternalApiGateway } from '../external-api.gateway';
import { ExchangeOracleGateway } from '../exchange-oracle.gateway';
import {
oracleStatsCommandFixture,
statisticsOracleUrl,
statisticsExchangeOracleUrl,
userStatsCommandFixture,
} from '../../../modules/statistics/spec/statistics.fixtures';
import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';
import nock, { RequestBodyMatcher } from 'nock';
import { of } from 'rxjs';
import { of, throwError } from 'rxjs';
import {
jobAssignmentCommandFixture,
jobAssignmentDataFixture,
jobAssignmentOracleUrl,
jobsFetchParamsCommandFixture,
jobsFetchParamsDataFixtureAsString,
} from '../../../modules/job-assignment/spec/job-assignment.fixtures';
import { ExternalApiProfile } from '../external-api.mapper';
import { ExchangeOracleProfile } from '../exchange-oracle.mapper';
import {
jobsDiscoveryParamsCommandFixture,
paramsDataFixtureAsString,
} from '../../../modules/jobs-discovery/spec/jobs-discovery.fixtures';
import { GoneException, HttpException } from '@nestjs/common';

describe('ExternalApiGateway', () => {
let gateway: ExternalApiGateway;
describe('ExchangeOracleApiGateway', () => {
let gateway: ExchangeOracleGateway;
let httpService: HttpService;

beforeEach(async () => {
Expand All @@ -35,8 +36,8 @@ describe('ExternalApiGateway', () => {
}),
],
providers: [
ExternalApiProfile,
ExternalApiGateway,
ExchangeOracleProfile,
ExchangeOracleGateway,
{
provide: HttpService,
useValue: {
Expand All @@ -46,7 +47,7 @@ describe('ExternalApiGateway', () => {
],
}).compile();

gateway = module.get<ExternalApiGateway>(ExternalApiGateway);
gateway = module.get<ExchangeOracleGateway>(ExchangeOracleGateway);
httpService = module.get<HttpService>(HttpService);
});

Expand All @@ -57,21 +58,49 @@ describe('ExternalApiGateway', () => {
describe('fetchUserStatistics', () => {
it('should successfully call the requested url for user statistics', async () => {
const command = userStatsCommandFixture;
nock(statisticsOracleUrl)
nock(statisticsExchangeOracleUrl)
.get('/stats/assignment')
.matchHeader('Authorization', `Bearer ${command.token}`)
.reply(200);
await gateway.fetchUserStatistics(command);
expect(httpService.request).toHaveBeenCalled();
});
it('should handle errors on fetchUserStatistics', async () => {
const command = {
exchangeOracleUrl: 'https://example.com',
token: 'dummyToken',
};
jest
.spyOn(httpService, 'request')
.mockReturnValue(
throwError(() => new HttpException('Service Unavailable', 503)),
);

await expect(gateway.fetchUserStatistics(command)).rejects.toThrow(
HttpException,
);
});
});
describe('fetchOracleStatistics', () => {
it('should successfully call the requested url for oracle statistics', async () => {
const command = oracleStatsCommandFixture;
nock(statisticsOracleUrl).get('/stats').reply(200);
nock(statisticsExchangeOracleUrl).get('/stats').reply(200);
await gateway.fetchOracleStatistics(command);
expect(httpService.request).toHaveBeenCalled();
});
it('should handle errors on fetchOracleStatistics', async () => {
const command = {
exchangeOracleUrl: 'https://example.com',
token: 'dummyToken',
};
jest
.spyOn(httpService, 'request')
.mockReturnValue(throwError(() => new GoneException()));

await expect(gateway.fetchUserStatistics(command)).rejects.toThrow(
GoneException,
);
});
});
describe('fetchAssignedJobs', () => {
it('should successfully call get assigned jobs', async () => {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { JobAssignmentService } from './job-assignment.service';
import { JobAssignmentProfile } from './job-assignment.mapper';
import { Module } from '@nestjs/common';
import { ExternalApiModule } from '../../integrations/external-api/external-api.module';
import { ExchangeOracleModule } from '../../integrations/exchange-oracle/exchange-oracle.module';

@Module({
imports: [ExternalApiModule],
imports: [ExchangeOracleModule],
providers: [JobAssignmentService, JobAssignmentProfile],
exports: [JobAssignmentService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import {
JobAssignmentCommand,
JobsFetchResponse,
} from './interfaces/job-assignment.interface';
import { ExternalApiGateway } from '../../integrations/external-api/external-api.gateway';
import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway';
@Injectable()
export class JobAssignmentService {
constructor(private readonly externalApiGateway: ExternalApiGateway) {}
constructor(private readonly gateway: ExchangeOracleGateway) {}

async processJobAssignment(
command: JobAssignmentCommand,
): Promise<JobAssignmentResponse> {
return this.externalApiGateway.postNewJobAssignment(command);
return this.gateway.postNewJobAssignment(command);
}

async processGetAssignedJobs(
command: JobsFetchParamsCommand,
): Promise<JobsFetchResponse> {
return this.externalApiGateway.fetchAssignedJobs(command);
return this.gateway.fetchAssignedJobs(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export class JobsDiscoveryController {
) {}

@ApiTags('Jobs-Discovery')
@Get('/discovery/jobs')
@Get('/jobs')
@ApiOperation({
summary:
'Retrieve a list of filtered available jobs for passed Exchange Oracle url',
})
public async discoverJobs(
public async getJobs(
@Query() jobsDiscoveryParamsDto: JobsDiscoveryParamsDto,
@Headers('authorization') token: string,
): Promise<JobsDiscoveryResponse> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { JobsDiscoveryService } from './jobs-discovery.service';
import { JobsDiscoveryProfile } from './jobs-discovery.mapper';
import { Module } from '@nestjs/common';
import { ExternalApiModule } from '../../integrations/external-api/external-api.module';
import { ExchangeOracleModule } from '../../integrations/exchange-oracle/exchange-oracle.module';

@Module({
imports: [ExternalApiModule],
imports: [ExchangeOracleModule],
providers: [JobsDiscoveryService, JobsDiscoveryProfile],
exports: [JobsDiscoveryService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
JobsDiscoveryParamsCommand,
JobsDiscoveryResponse,
} from './interfaces/jobs-discovery.interface';
import { ExternalApiGateway } from '../../integrations/external-api/external-api.gateway';
import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway';
@Injectable()
export class JobsDiscoveryService {
constructor(private readonly externalApiGateway: ExternalApiGateway) {}
constructor(private readonly gateway: ExchangeOracleGateway) {}

async processJobsDiscovery(
command: JobsDiscoveryParamsCommand,
): Promise<JobsDiscoveryResponse> {
return this.externalApiGateway.fetchDiscoveredJobs(command);
return this.gateway.fetchDiscoveredJobs(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('JobsDiscoveryController', () => {
it('should call service processJobsDiscovery method with proper fields set', async () => {
const dto = dtoFixture;
const command = jobsDiscoveryParamsCommandFixture;
await controller.discoverJobs(dto, jobDiscoveryToken);
await controller.getJobs(dto, jobDiscoveryToken);
expect(jobsDiscoveryService.processJobsDiscovery).toHaveBeenCalledWith(
command,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export class OracleDiscoveryController {
@InjectMapper() private readonly mapper: Mapper,
) {}
@ApiTags('Oracle-Discovery')
@Get('/discovery/oracles')
@Get('/oracles')
@ApiOperation({ summary: 'Oracles discovery' })
@UsePipes(new ValidationPipe())
public oracleDiscovery(
public getOracles(
@Body() oracleDiscoveryDto: OracleDiscoveryDto,
): Promise<OracleDiscoveryResponse[]> {
const oracleDiscoveryCommand = this.mapper.map(
Expand Down
Loading
Loading