Skip to content

Commit

Permalink
code changes to align behaviour of this module with postman-paf-java …
Browse files Browse the repository at this point in the history
…(java implementation)
  • Loading branch information
harvey-dvla committed Mar 26, 2024
1 parent b172fd5 commit 3bd2dc3
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 71 deletions.
81 changes: 73 additions & 8 deletions src/__test__/addressConversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,86 @@ describe('convertStructuredToUnstructured', () => {
});
});

it('throws error when unable to apply rules', () => {
it('converts structuredAddress when no rules apply thoroughfareName', () => {
// Given
const structuredAddress = {
thoroughfareName: structuredAddressTemplate.thoroughfareName,
...postTownAndPostcode,
};

// When
const convertedUnstructuredAddress = convertStructuredToUnstructured(structuredAddress);

// Then
expect(convertedUnstructuredAddress).toEqual({
line1: structuredAddress.thoroughfareName,
...line5AndPostcode,
});
});

it('converts structuredAddress when no rules apply dependant thoroughfareName', () => {
// Given
const structuredAddress = {
dependentThoroughfareName: structuredAddressTemplate.dependentThoroughfareName,
...postTownAndPostcode,
};

// When
const convertedUnstructuredAddress = convertStructuredToUnstructured(structuredAddress);

// Then
expect(convertedUnstructuredAddress).toEqual({
line1: structuredAddress.dependentThoroughfareName,
...line5AndPostcode,
});
});

it('converts structuredAddress when no rules apply dependant doubleDependentLocality', () => {
// Given
const structuredAddress = {
doubleDependentLocality: structuredAddressTemplate.doubleDependentLocality,
...postTownAndPostcode,
};

// When
const convertedUnstructuredAddress = convertStructuredToUnstructured(structuredAddress);

// Then
expect(convertedUnstructuredAddress).toEqual({
line1: structuredAddress.doubleDependentLocality,
...line5AndPostcode,
});
});

it('converts structuredAddress when no rules apply dependant dependentLocality', () => {
// Given
const structuredAddress = {
dependentLocality: structuredAddressTemplate.dependentLocality,
...postTownAndPostcode,
};

// When
let error;
try {
convertStructuredToUnstructured(structuredAddress);
} catch (err) {
error = err;
}
const convertedUnstructuredAddress = convertStructuredToUnstructured(structuredAddress);

// Then
expect(error.message).toEqual('Structured address did not match any of the conversion rules');
expect(convertedUnstructuredAddress).toEqual({
line1: structuredAddress.dependentLocality,
...line5AndPostcode,
});
});

it('returns posttown and postcode correctly when only postcode and postTown supplied', () => {
// Given
const structuredAddress = {
...postTownAndPostcode,
};

// When
const convertedUnstructuredAddress = convertStructuredToUnstructured(structuredAddress);

// Then
expect(convertedUnstructuredAddress).toEqual({
...line5AndPostcode,
});
});
});
3 changes: 2 additions & 1 deletion src/addressConversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
convertRule5StructuredToUnstructured,
convertRule6StructuredToUnstructured,
convertRule7StructuredToUnstructured,
convertNoRuleApplicableStructuredToUnstructured,
} = require('./conversion-rules/ruleConversions');

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ const convertStructuredToUnstructured = (structuredAddress) => {
return convertRule7StructuredToUnstructured(structuredAddress);
}

throw new Error('Structured address did not match any of the conversion rules');
return convertNoRuleApplicableStructuredToUnstructured(structuredAddress);
};

module.exports = {
Expand Down
27 changes: 27 additions & 0 deletions src/conversion-rules/__test__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ const structuredAddressTemplate = {
postcode: 'AC1 2DC',
};

const structuredAddressNoPostcodeOrPosttownTemplate = {
organisationName: 'BUSY BUSINESS',
departmentName: 'DEPARTMENT OF DEFENCE AGAINST FAILED TESTS',
poBoxNumber: '12345',
buildingName: 'BOB',
buildingNumber: '1',
subBuildingName: 'FLAT 1',
thoroughfareName: 'DRIVE-THROUGH THOROUGHFARE',
dependentThoroughfareName: '"IT DEPENDS..." ROAD',
dependentLocality: 'NOWHERE',
doubleDependentLocality: 'TWICE NOWHERE IS STILL NOWHERE...',
postTown: '',
postcode: '',
};

const structuredAddressBuildingNumberAndPoBoxNumberAreNumbersTemplate = {
poBoxNumber: 12345,
buildingNumber: 5,
thoroughfareName: 'DRIVE-THROUGH THOROUGHFARE',
dependentLocality: 'NOWHERE',
doubleDependentLocality: 'TWICE NOWHERE IS STILL NOWHERE...',
postTown: 'A TOWN FULL OF MAIL',
postcode: 'AC1 2DC',
};

const internationalAddressTemplate = {
line1: 'El Quinto Pino line1',
line2: 'El Quinto Pino line2',
Expand Down Expand Up @@ -59,6 +84,8 @@ const numericPartExceptionElements = {

module.exports = {
structuredAddressTemplate,
structuredAddressNoPostcodeOrPosttownTemplate,
structuredAddressBuildingNumberAndPoBoxNumberAreNumbersTemplate,
internationalAddressTemplate,
bfpoAddressTemplate,
postTownAndPostcode,
Expand Down
40 changes: 40 additions & 0 deletions src/conversion-rules/__test__/ruleConversions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
convertRule5StructuredToUnstructured,
convertRule6StructuredToUnstructured,
convertRule7StructuredToUnstructured,
convertNoRuleApplicableStructuredToUnstructured,
} = require('../ruleConversions');
const {
structuredAddressTemplate,
Expand Down Expand Up @@ -1138,4 +1139,43 @@ describe('convertRule7StructuredToUnstructured', () => {
...line5AndPostcode,
});
});

it('add address elements to line 1 and 2 when building number present', () => {
// Given
const structuredAddress = {
departmentName: structuredAddressTemplate.departmentName,
buildingNumber: structuredAddressTemplate.buildingNumber,
...postTownAndPostcode,
};

// When
const convertedUnstructuredAddress = convertNoRuleApplicableStructuredToUnstructured(structuredAddress);

// Then
expect(convertedUnstructuredAddress).toEqual({
line1: structuredAddressTemplate.departmentName,
line2: structuredAddressTemplate.buildingNumber,
...line5AndPostcode,
});
});

it('add address elements to line 1 and 2 when building number not present', () => {
// Given
const structuredAddress = {
departmentName: structuredAddressTemplate.departmentName,
thoroughfareName: structuredAddressTemplate.thoroughfareName,

...postTownAndPostcode,
};

// When
const convertedUnstructuredAddress = convertNoRuleApplicableStructuredToUnstructured(structuredAddress);

// Then
expect(convertedUnstructuredAddress).toEqual({
line1: structuredAddressTemplate.departmentName,
line2: structuredAddressTemplate.thoroughfareName,
...line5AndPostcode,
});
});
});
56 changes: 0 additions & 56 deletions src/conversion-rules/__test__/ruleConversionsHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,62 +233,6 @@ describe('mapAddressElementsToUnstructuredAddress', () => {
// Then
expect(mappedUnstructuredAddress).toEqual(testAddressMapping);
});

it('throws an error if addressElements is an empty array', () => {
// Given
const addressElements = [];
let error;

// When
try {
mapAddressElementsToUnstructuredAddress(
addressElements,
testAddressToMap.postTown,
testAddressToMap.postcode
);
} catch (err) {
error = err;
}

// Then
expect(error.message).toEqual(
'Cannot convert an address without at least 1 element other than postTown and postcode.'
);
});

it('throws an error if postTown is null', () => {
// Given
const addressElements = testAddressToMap.elements;
const postTown = null;
let error;

// When
try {
mapAddressElementsToUnstructuredAddress(addressElements, postTown, testAddressToMap.postcode);
} catch (err) {
error = err;
}

// Then
expect(error.message).toEqual('Cannot convert an address without a postTown or postcode.');
});

it('throws an error if postcode is null', () => {
// Given
const addressElements = testAddressToMap.elements;
const postcode = null;
let error;

// When
try {
mapAddressElementsToUnstructuredAddress(addressElements, testAddressToMap.postTown, postcode);
} catch (err) {
error = err;
}

// Then
expect(error.message).toEqual('Cannot convert an address without a postTown or postcode.');
});
});

describe('isExceptionRuleIndicator', () => {
Expand Down
26 changes: 26 additions & 0 deletions src/conversion-rules/ruleConversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
addPresentAddressElement,
mapAddressElementsToUnstructuredAddress,
addThoroughfareAndLocalityElementsWithPremisesPrefix,
addAllPresentOrganisationElementsAndBuildingNames,
isExceptionRuleIndicator,
isBuildingNameNumericPartException,
getNamePartOfBuildingName,
Expand Down Expand Up @@ -245,6 +246,30 @@ const convertRule7StructuredToUnstructured = (structuredAddress) => {
);
};

/**
* Convert a structured address to an unstructured address when none of the 7 rules apply
*
* @param structuredAddress {{}} The structured address to be converted
* @returns {{}} The converted unstructured address
*/
const convertNoRuleApplicableStructuredToUnstructured = (structuredAddress) => {
const addressElements = [];

addAllPresentOrganisationElementsAndBuildingNames(addressElements, structuredAddress);

if (structuredAddress.buildingNumber) {
addPresentAddressElement(addressElements, structuredAddress.buildingNumber);
} else {
addAllPresentThoroughfareAndLocalityElements(addressElements, structuredAddress);
}

return mapAddressElementsToUnstructuredAddress(
addressElements,
structuredAddress.postTown,
structuredAddress.postcode
);
};

module.exports = {
convertRule1StructuredToUnstructured,
convertRule2StructuredToUnstructured,
Expand All @@ -253,4 +278,5 @@ module.exports = {
convertRule5StructuredToUnstructured,
convertRule6StructuredToUnstructured,
convertRule7StructuredToUnstructured,
convertNoRuleApplicableStructuredToUnstructured,
};
32 changes: 26 additions & 6 deletions src/conversion-rules/ruleConversionsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ const addAllPresentOrganisationElements = (addressElements, structuredAddress) =
addPresentAddressElement(addressElements, formatPoBoxNumber(structuredAddress.poBoxNumber));
};

/**
* Add all present organisation elements (organisation name, department name and PO Box number) and
* building names elements (building name and sub building name) to the address elements array.
*
* @param {*} addressElements {string[]} The array of address elements to append the organisation elements to
* @param {*} structuredAddress {{}} The structured address being converted
*/
const addAllPresentOrganisationElementsAndBuildingNames = (addressElements, structuredAddress) => {
addPresentAddressElement(addressElements, structuredAddress.organisationName);
addPresentAddressElement(addressElements, structuredAddress.departmentName);
addPresentAddressElement(addressElements, structuredAddress.buildingName);
addPresentAddressElement(addressElements, structuredAddress.subBuildingName);
addPresentAddressElement(addressElements, formatPoBoxNumber(structuredAddress.poBoxNumber));
};

/**
* Add all present thoroughfare and locality elements (thoroughfare name, dependent thoroughfare name, dependent
* locality and double dependent locality) to the address elements array.
Expand Down Expand Up @@ -103,12 +118,16 @@ const addAllPresentThoroughfareAndLocalityElements = (addressElements, structure
* @returns {{}} Mapped unstructured address
*/
const mapAddressElementsToUnstructuredAddress = (addressElements, postTown, postcode) => {
if (addressElements.length < 1) {
throw new Error('Cannot convert an address without at least 1 element other than postTown and postcode.');
}
if (!postTown || !postcode) {
throw new Error('Cannot convert an address without a postTown or postcode.');
}
// Errors removed from module to replicate behaviour of the postman-paf-java module
// Code commented out as the errors might be used in a future iteration
// of the library

// if (addressElements.length < 1) {
// throw new Error('Cannot convert an address without at least 1 element other than postTown and postcode.');
// }
// if (!postTown || !postcode) {
// throw new Error('Cannot convert an address without a postTown or postcode.');
// }

const convertedUnstructuredAddress = {};

Expand Down Expand Up @@ -252,6 +271,7 @@ module.exports = {
addPresentAddressElement,
addThoroughfareAndLocalityElementsWithPremisesPrefix,
addAllPresentOrganisationElements,
addAllPresentOrganisationElementsAndBuildingNames,
addAllPresentThoroughfareAndLocalityElements,
mapAddressElementsToUnstructuredAddress,
isExceptionRuleIndicator,
Expand Down

0 comments on commit 3bd2dc3

Please sign in to comment.