Skip to content

Commit

Permalink
added typedi and refactored container
Browse files Browse the repository at this point in the history
  • Loading branch information
me-matt committed Sep 26, 2024
1 parent 9e5c900 commit b05128e
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 92 deletions.
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"moment": "^2.24.0",
"node-yaml": "^4.0.1",
"reflect-metadata": "^0.1.13",
"ts-node-register": "^1.0.0"
"ts-node-register": "^1.0.0",
"typedi": "^0.10.0"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
Expand Down
8 changes: 3 additions & 5 deletions src/functions/certGen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeleteObjectCommandOutput, PutObjectCommandOutput } from '@aws-sdk/client-s3';
import { Callback, Context, Handler, SQSEvent, SQSRecord } from 'aws-lambda';
import { Container } from 'typedi';
import { ERRORS } from '../models/Enums';
import { Injector } from '../models/injector/Injector';
import { CertificateGenerationService, IGeneratedCertificateResponse } from '../services/CertificateGenerationService';
import { CertificateUploadService } from '../services/CertificateUploadService';

Expand All @@ -19,10 +19,8 @@ const certGen: Handler = async (event: SQSEvent, context?: Context, callback?: C
throw new Error('Event is empty');
}

const certificateGenerationService: CertificateGenerationService =
Injector.resolve<CertificateGenerationService>(CertificateGenerationService);
const certificateUploadService: CertificateUploadService =
Injector.resolve<CertificateUploadService>(CertificateUploadService);
const certificateGenerationService: CertificateGenerationService = Container.get(CertificateGenerationService);
const certificateUploadService: CertificateUploadService = Container.get(CertificateUploadService);
const certificateUploadPromises: Array<Promise<CertGenReturn>> = [];

event.Records.forEach((record: SQSRecord) => {
Expand Down
9 changes: 0 additions & 9 deletions src/models/injector/GenericClassDecorator.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/models/injector/Injector.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/models/injector/ServiceDecorator.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/services/CertificateGenerationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GetObjectOutput } from '@aws-sdk/client-s3';
import { getProfile } from '@dvsa/cvs-feature-flags/profiles/vtx';
import { toUint8Array } from '@smithy/util-utf8';
import moment from 'moment';
import { Inject, Service } from 'typedi';
import {
ICertificatePayload,
ICustomDefect,
Expand Down Expand Up @@ -41,7 +42,6 @@ import { IFlatDefect } from '../models/IFlatDefect';
import { IItem } from '../models/IItem';
import { ITestStation } from '../models/ITestStations';
import { ISearchResult, TechRecordGet, TechRecordType } from '../models/Types';
import { Service } from '../models/injector/ServiceDecorator';
import { Configuration } from '../utils/Configuration';
import { LambdaService } from './LambdaService';
import { S3BucketService } from './S3BucketService';
Expand All @@ -55,7 +55,7 @@ class CertificateGenerationService {
private readonly config: Configuration;
private readonly lambdaClient: LambdaService;

constructor(s3Client: S3BucketService, lambdaClient: LambdaService) {
constructor(@Inject() s3Client: S3BucketService, @Inject() lambdaClient: LambdaService) {
this.s3Client = s3Client;
this.config = Configuration.getInstance();
this.lambdaClient = lambdaClient;
Expand Down
4 changes: 2 additions & 2 deletions src/services/CertificateUploadService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PutObjectCommandOutput } from '@aws-sdk/client-s3';
import { Inject, Service } from 'typedi';
import { IGeneratedCertificateResponse } from '../models';
import { Service } from '../models/injector/ServiceDecorator';
import { S3BucketService } from './S3BucketService';

/**
Expand All @@ -10,7 +10,7 @@ import { S3BucketService } from './S3BucketService';
class CertificateUploadService {
private readonly s3BucketService: S3BucketService;

constructor(s3BucketService: S3BucketService) {
constructor(@Inject() s3BucketService: S3BucketService) {
this.s3BucketService = s3BucketService;
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/LambdaService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InvocationRequest, InvocationResponse, InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { Inject, Service } from 'typedi';
import { IInvokeConfig } from '../models';
import { ERRORS } from '../models/Enums';
import { HTTPError } from '../models/HTTPError';
import { Service } from '../models/injector/ServiceDecorator';
import { Configuration } from '../utils/Configuration';

import AWSXRay from 'aws-xray-sdk';
Expand All @@ -14,7 +14,7 @@ import AWSXRay from 'aws-xray-sdk';
class LambdaService {
public readonly lambdaClient: LambdaClient;

constructor(lambdaClient: LambdaClient) {
constructor(@Inject() lambdaClient: LambdaClient) {
const config: IInvokeConfig = Configuration.getInstance().getInvokeConfig();
this.lambdaClient = AWSXRay.captureAWSv3Client(new LambdaClient({ ...lambdaClient, ...config.params }));
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/S3BucketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
S3Client,
} from '@aws-sdk/client-s3';
import AWSXRay from 'aws-xray-sdk';
import { Inject, Service } from 'typedi';
import { IS3Config } from '../models';
import { Service } from '../models/injector/ServiceDecorator';
import { Configuration } from '../utils/Configuration';

/**
Expand All @@ -20,7 +20,7 @@ import { Configuration } from '../utils/Configuration';
class S3BucketService {
public readonly s3Client: S3Client;

constructor(s3Client: S3Client) {
constructor(@Inject() s3Client: S3Client) {
const config: IS3Config = Configuration.getInstance().getS3Config();
this.s3Client = AWSXRay.captureAWSv3Client(new S3Client({ ...s3Client, ...config }));
}
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/certGen.intTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import 'reflect-metadata';

import { Container } from 'typedi';
import { CertificateGenerationService } from "../../src/services/CertificateGenerationService";
import { CertificateUploadService } from "../../src/services/CertificateUploadService";
import { certGen } from "../../src/functions/certGen";
import lambdaTester from "lambda-tester";
import sinon from "sinon";
import { S3BucketService } from "../../src/services/S3BucketService";
import { LambdaService } from "../../src/services/LambdaService";
import { S3BucketMockService } from "../models/S3BucketMockService";
import { LambdaMockService } from "../models/LambdaMockService";
// tslint:disable:max-line-length

describe("Invoke certGen Function", () => {
Container.set(S3BucketService, new S3BucketMockService());
Container.set(LambdaService, new LambdaMockService());
const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
});
Expand Down
2 changes: 2 additions & 0 deletions tests/models/LambdaMockService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Service } from "typedi";
import { ServiceException } from "@smithy/smithy-client";
import { InvocationRequest, InvocationResponse } from "@aws-sdk/client-lambda";
import { Configuration } from "../../src/utils/Configuration";
Expand All @@ -13,6 +14,7 @@ interface IMockFunctions {
/**
* Service for mocking the LambdaService
*/
@Service()
class LambdaMockService {
private static responses: IMockFunctions[] = [];

Expand Down
2 changes: 2 additions & 0 deletions tests/models/S3BucketMockService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Service } from "typedi";
import {GetObjectOutput, PutObjectCommandOutput} from "@aws-sdk/client-s3";
import {Readable} from "stream";
import * as fs from "fs";
Expand All @@ -11,6 +12,7 @@ interface IBucket {
/**
* Service for mocking the S3BucketService
*/
@Service()
class S3BucketMockService {
public static buckets: IBucket[] = [];

Expand Down
16 changes: 13 additions & 3 deletions tests/unit/CertificateGenerationService.unitTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'reflect-metadata';

/* eslint-disable import/first */
const mockGetProfile = jest.fn();

import Container from "typedi";
import { LambdaClient } from "@aws-sdk/client-lambda";
import { cloneDeep } from "lodash";
import sinon from "sinon";
Expand All @@ -9,9 +12,9 @@ import { HTTPError } from "../../src/models/HTTPError";
import { IDefectParent } from "../../src/models/IDefectParent";
import { CertificateGenerationService } from "../../src/services/CertificateGenerationService";
import { LambdaService } from "../../src/services/LambdaService";
import defectsMock from "../../tests/resources/defects_mock.json";
import flatDefectsMock from "../../tests/resources/flattened-defects.json";
import testStationsMock from "../../tests/resources/testStationsMock.json";
import defectsMock from "../resources/defects_mock.json";
import flatDefectsMock from "../resources/flattened-defects.json";
import testStationsMock from "../resources/testStationsMock.json";
import queueEventFail from "../resources/queue-event-fail.json";
import queueEventPass from "../resources/queue-event-pass.json";
import queueEventPRS from "../resources/queue-event-prs.json";
Expand All @@ -29,13 +32,20 @@ import testResultsRespFail from "../resources/test-results-fail-response.json";
import testResultsRespNoCert from "../resources/test-results-nocert-response.json";
import testResultsRespPrs from "../resources/test-results-prs-response.json";
import testResultsResp from "../resources/test-results-response.json";
import { S3BucketService } from "../../src/services/S3BucketService";
import { S3BucketMockService } from "../models/S3BucketMockService";
import { LambdaMockService } from "../models/LambdaMockService";

jest.mock("@dvsa/cvs-feature-flags/profiles/vtx", () => ({
getProfile: mockGetProfile
}));

describe("Certificate Generation Service", () => {
Container.set(S3BucketService, S3BucketMockService);
Container.set(LambdaService, LambdaMockService);

const sandbox = sinon.createSandbox();

afterEach(() => {
sandbox.restore();
});
Expand Down
29 changes: 12 additions & 17 deletions tests/unit/certGen.unitTest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import 'reflect-metadata';

/* eslint-disable import/first */
const mockGetProfile = jest.fn();

import * as fs from "fs";
import { cloneDeep } from "lodash";
import * as path from "path";
import { Container } from "typedi";
import sinon from "sinon";
import { certGen } from "../../src/functions/certGen";
import { ICertificatePayload, IFeatureFlags, ITestResult } from "../../src/models";
import { Injector } from "../../src/models/injector/Injector";
import {
CertificateGenerationService,
IGeneratedCertificateResponse,
Expand All @@ -28,6 +30,8 @@ import techRecordsRwtHgv from "../resources/tech-records-response-rwt-hgv.json";
import techRecordsRwtSearch from "../resources/tech-records-response-rwt-search.json";
import techRecordsRwt from "../resources/tech-records-response-rwt.json";
import techRecordsSearchPsv from "../resources/tech-records-response-search-PSV.json";
import { S3BucketService } from "../../src/services/S3BucketService";
import { LambdaService } from "../../src/services/LambdaService";

const sandbox = sinon.createSandbox();

Expand All @@ -39,11 +43,12 @@ describe("cert-gen", () => {
it("should pass", () => {
expect(true).toBe(true);
});
const certificateGenerationService: CertificateGenerationService =
Injector.resolve<CertificateGenerationService>(
CertificateGenerationService,
[S3BucketMockService, LambdaMockService]
);

Container.set(S3BucketService, new S3BucketMockService());
Container.set(LambdaService, new LambdaMockService());

const certificateGenerationService = Container.get(CertificateGenerationService);

beforeAll(() => {
jest.setTimeout(10000);
});
Expand Down Expand Up @@ -8109,17 +8114,7 @@ describe("cert-gen", () => {
)
);
const testResult: any = JSON.parse(event.Records[0].body);
const certificateUploadService: CertificateUploadService =
Injector.resolve<CertificateUploadService>(CertificateUploadService, [
S3BucketMockService,
]);

// tslint:disable-next-line:no-shadowed-variable
const certificateGenerationService: CertificateGenerationService =
Injector.resolve<CertificateGenerationService>(
CertificateGenerationService,
[S3BucketMockService, LambdaMockService]
);
const certificateUploadService = Container.get(CertificateUploadService);

context("when uploading a certificate", () => {
context("and the S3 bucket exists and is accesible", () => {
Expand Down
24 changes: 14 additions & 10 deletions tests/unit/getWeightDetails.unitTest.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { Injector } from "../../src/models/injector/Injector";
import 'reflect-metadata';

import { Container } from "typedi";
import { cloneDeep } from "lodash";
import sinon from "sinon";
import { CertificateGenerationService } from "../../src/services/CertificateGenerationService";
import { S3BucketMockService } from "../models/S3BucketMockService";
import { LambdaMockService } from "../models/LambdaMockService";
import sinon from "sinon";
import queueEventPass from "../resources/queue-event-pass.json";
import techRecordsRwt from "../resources/tech-records-response-rwt.json";
import techRecordsRwtSearch from "../resources/tech-records-response-rwt-search.json";

const sandbox = sinon.createSandbox();
import { cloneDeep } from "lodash";
import { IWeightDetails, ITestResult } from "../../src/models";
import { HTTPError } from "../../src/models/HTTPError";
import { S3BucketService } from "../../src/services/S3BucketService";
import { LambdaService } from "../../src/services/LambdaService";

const sandbox = sinon.createSandbox();

describe("cert-gen", () => {
const certificateGenerationService: CertificateGenerationService =
Injector.resolve<CertificateGenerationService>(
CertificateGenerationService,
[S3BucketMockService, LambdaMockService]
);
Container.set(S3BucketService, new S3BucketMockService());
Container.set(LambdaService, new LambdaMockService());

const certificateGenerationService = Container.get(CertificateGenerationService);

afterEach(() => {
sandbox.restore();
});
Expand Down

0 comments on commit b05128e

Please sign in to comment.