Skip to content

Commit

Permalink
feat(DTFS2-7052): tests for ordnance survey API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
avaitonis committed Apr 15, 2024
1 parent c91144d commit 837a482
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 18 deletions.
62 changes: 62 additions & 0 deletions src/modules/geospatial/geospatial.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { GEOSPATIAL } from '@ukef/constants';
import { GetGeospatialAddressesGenerator } from '@ukef-test/support/generator/get-geospatial-addresses-generator';
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator';
import { resetAllWhenMocks, when } from 'jest-when';

import { GeospatialController } from './geospatial.controller';
import { GeospatialService } from './geospatial.service';

describe('GeospatialController', () => {
let geospatialServiceGetAddressesByPostcode: jest.Mock;

let controller: GeospatialController;

const valueGenerator = new RandomValueGenerator();
const { getAddressByPostcodeResponse, getAddressByPostcodeMultipleResponse } = new GetGeospatialAddressesGenerator(valueGenerator).generate({
numberToGenerate: 2,
});

beforeEach(() => {
resetAllWhenMocks();
const geospatialService = new GeospatialService(null);
geospatialServiceGetAddressesByPostcode = jest.fn();
geospatialService.getAddressesByPostcode = geospatialServiceGetAddressesByPostcode;

controller = new GeospatialController(geospatialService);
});

it('should be defined', () => {
expect(GeospatialController).toBeDefined();
});

describe('getAddressesByPostcode()', () => {
const postcode = GEOSPATIAL.EXAMPLES.POSTCODE;

it('returns address for postcode', async () => {
when(geospatialServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressByPostcodeResponse[0]);

const response = await controller.getAddressesByPostcode({ postcode });

expect(geospatialServiceGetAddressesByPostcode).toHaveBeenCalled();
expect(response).toEqual(getAddressByPostcodeResponse[0]);
});

it('returns multiple addressess for postcode', async () => {
when(geospatialServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressByPostcodeMultipleResponse);

const response = await controller.getAddressesByPostcode({ postcode });

expect(geospatialServiceGetAddressesByPostcode).toHaveBeenCalled();
expect(response).toEqual(getAddressByPostcodeMultipleResponse);
});

it('returns empty response for postcode', async () => {
when(geospatialServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce([]);

const response = await controller.getAddressesByPostcode({ postcode });

expect(geospatialServiceGetAddressesByPostcode).toHaveBeenCalled();
expect(response).toEqual([]);
});
});
});
7 changes: 4 additions & 3 deletions src/modules/geospatial/geospatial.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ export class GeospatialController {

@Get('addresses/postcode')
@ApiOperation({
summary: "A search based on a property's postcode. Will accept a full postcode consisting of the area, district, sector and unit e.g. SO16 0AS.",
summary:
"A search based on a property's postcode. Will accept a full valid postcode. Returns addresses from Ordanance survey Delivery Point Address (DPA) system.",
})
@ApiResponse({
status: 200,
description: 'Returns addresses from Ordanance survey Delivery Point Address (DPA) system.',
description: 'Returns simplified addresses that are ready to show to users.',
type: [GetAddressesResponseItem],
})
@ApiNotFoundResponse({
description: 'Customer not found.',
})
getGeospatial(@Query() query: GetAddressByPostcodeQueryDto): Promise<GetAddressesResponse> {
getAddressesByPostcode(@Query() query: GetAddressByPostcodeQueryDto): Promise<GetAddressesResponse> {
return this.geospatialService.getAddressesByPostcode(query.postcode);
}
}
67 changes: 67 additions & 0 deletions src/modules/geospatial/geospatial.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ConfigService } from '@nestjs/config';
import { OrdnanceSurveyService } from '@ukef/helper-modules/ordnance-survey/ordnance-survey.service';
import { GetGeospatialAddressesGenerator } from '@ukef-test/support/generator/get-geospatial-addresses-generator';
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator';
import { resetAllWhenMocks, when } from 'jest-when';

import { GeospatialService } from './geospatial.service';

jest.mock('@ukef/modules/informatica/informatica.service');

describe('CustomerService', () => {
const valueGenerator = new RandomValueGenerator();

let service: GeospatialService;
let configServiceGet: jest.Mock;
let informaticaServiceGetAddressesByPostcode: jest.Mock;

beforeEach(() => {
const configService = new ConfigService();
configServiceGet = jest.fn().mockReturnValue({ key: valueGenerator.word() });
configService.get = configServiceGet;

informaticaServiceGetAddressesByPostcode = jest.fn();
const ordnanceSurveyService = new OrdnanceSurveyService(null, configService);
ordnanceSurveyService.getAddressesByPostcode = informaticaServiceGetAddressesByPostcode;
resetAllWhenMocks();

service = new GeospatialService(ordnanceSurveyService);
});

describe('getAddressesByPostcode', () => {
const {
getAddressByPostcodeResponse,
getAddressByPostcodeMultipleResponse,
getAddressOrdnanceSurveyResponse,
getAddressOrdnanceSurveyMultipleResponse,
getAddressOrdnanceSurveyEmptyResponse,
} = new GetGeospatialAddressesGenerator(valueGenerator).generate({
numberToGenerate: 2,
});
const postcode = getAddressByPostcodeResponse[0][0].postalCode;

it('returns address from the backend service', async () => {
when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressOrdnanceSurveyResponse[0]);

const response = await service.getAddressesByPostcode(postcode);

expect(response).toEqual(getAddressByPostcodeResponse[0]);
});

it('returns multiple addressess from the backend service', async () => {
when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressOrdnanceSurveyMultipleResponse);

const response = await service.getAddressesByPostcode(postcode);

expect(response).toEqual(getAddressByPostcodeMultipleResponse);
});

it('can handle empty backend response', async () => {
when(informaticaServiceGetAddressesByPostcode).calledWith(postcode).mockResolvedValueOnce(getAddressOrdnanceSurveyEmptyResponse[0]);

const response = await service.getAddressesByPostcode(postcode);

expect(response).toEqual([]);
});
});
});
2 changes: 1 addition & 1 deletion src/modules/geospatial/geospatial.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class GeospatialService {
addressLine3: null,
locality: item_data.POST_TOWN || null,
postalCode: item_data.POSTCODE || null,
country: ENUMS.GEOSPATIAL_COUNTRIES[item_data.COUNTRY_CODE],
country: ENUMS.GEOSPATIAL_COUNTRIES[item_data.COUNTRY_CODE] || null,
});
});

Expand Down
11 changes: 5 additions & 6 deletions test/docs/__snapshots__/get-docs-yaml.api-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,11 @@ paths:
- yield-rates
/api/v1/geospatial/addresses/postcode:
get:
operationId: GeospatialController_getGeospatial
operationId: GeospatialController_getAddressesByPostcode
summary: >-
A search based on a property's postcode. Will accept a full postcode
consisting of the area, district, sector and unit e.g. SO16 0AS.
A search based on a property's postcode. Will accept a full valid
postcode. Returns addresses from Ordanance survey Delivery Point Address
(DPA) system.
parameters:
- name: postcode
required: true
Expand All @@ -502,9 +503,7 @@ paths:
type: string
responses:
'200':
description: >-
Returns addresses from Ordanance survey Delivery Point Address (DPA)
system.
description: Returns simplified addresses that are ready to show to users.
content:
application/json:
schema:
Expand Down
7 changes: 4 additions & 3 deletions test/geospatial/get-address-by-postcode.api-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ describe('GET /geospatial/addresses/postcode?postcode=', () => {
ordnanceSurveyPath,
mdmPath,
getAddressByPostcodeResponse,
getAddressByPostcodeMultipleResponse,
getAddressOrdnanceSurveyResponse,
getAddressOrdnanceSurveyEmptyResponse,
getAddressessOrdnanceSurveyResponse,
getAddressOrdnanceSurveyMultipleResponse,
ordnanceSurveyAuthErrorResponse,
} = new GetGeospatialAddressesGenerator(valueGenerator).generate({
postcode: GEOSPATIAL.EXAMPLES.POSTCODE,
Expand Down Expand Up @@ -58,12 +59,12 @@ describe('GET /geospatial/addresses/postcode?postcode=', () => {
});

it('returns a 200 response with the addresses if they are returned by Ordnance Survey API', async () => {
requestToGetAddressesByPostcode(ordnanceSurveyPath[0]).reply(200, getAddressessOrdnanceSurveyResponse);
requestToGetAddressesByPostcode(ordnanceSurveyPath[0]).reply(200, getAddressOrdnanceSurveyMultipleResponse);

const { status, body } = await api.get(mdmPath[0]);

expect(status).toBe(200);
expect(body).toStrictEqual([getAddressByPostcodeResponse[0][0], getAddressByPostcodeResponse[1][0]]);
expect(body).toStrictEqual(getAddressByPostcodeMultipleResponse);
});

it('returns a empty 200 response if Ordnance Survey API returns a 200 without results', async () => {
Expand Down
12 changes: 7 additions & 5 deletions test/support/generator/get-geospatial-addresses-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class GetGeospatialAddressesGenerator extends AbstractGenerator<AddressVa
},
]);

const getAddressByPostcodeMultipleResponse = getAddressByPostcodeResponse.map((response) => response[0]);

const getAddressOrdnanceSurveyResponse: GetAddressOrdnanceSurveyResponse[] = values.map((v) => ({
header: {
uri: 'test',
Expand Down Expand Up @@ -107,7 +109,7 @@ export class GetGeospatialAddressesGenerator extends AbstractGenerator<AddressVa
],
}));

const getAddressessOrdnanceSurveyResponse: GetAddressOrdnanceSurveyResponse = {
const getAddressOrdnanceSurveyMultipleResponse: GetAddressOrdnanceSurveyResponse = {
header: {
uri: 'test',
query: 'test',
Expand Down Expand Up @@ -187,13 +189,13 @@ export class GetGeospatialAddressesGenerator extends AbstractGenerator<AddressVa

return {
request,
// ordnanceSurveyRequest,
ordnanceSurveyPath,
mdmPath,
getAddressByPostcodeResponse,
getAddressByPostcodeMultipleResponse,
getAddressOrdnanceSurveyResponse,
getAddressOrdnanceSurveyEmptyResponse,
getAddressessOrdnanceSurveyResponse,
getAddressOrdnanceSurveyMultipleResponse,
ordnanceSurveyAuthErrorResponse,
};
}
Expand All @@ -217,12 +219,12 @@ interface GenerateOptions {

interface GenerateResult {
request: GetAddressByPostcodeQueryDto[];
//ordnanceSurveyRequest: GetCustomersInformaticaQueryDto[];
ordnanceSurveyPath: string[];
mdmPath: string[];
getAddressByPostcodeResponse: GetAddressesResponse[];
getAddressByPostcodeMultipleResponse: GetAddressesResponse;
getAddressOrdnanceSurveyResponse: GetAddressOrdnanceSurveyResponse[];
getAddressessOrdnanceSurveyResponse: GetAddressOrdnanceSurveyResponse;
getAddressOrdnanceSurveyMultipleResponse: GetAddressOrdnanceSurveyResponse;
getAddressOrdnanceSurveyEmptyResponse: GetAddressOrdnanceSurveyResponse[];
ordnanceSurveyAuthErrorResponse: OrdnanceSurveyAuthErrorResponse;
}

0 comments on commit 837a482

Please sign in to comment.