From 81c7601325fc8e271191be53175c47ed5de750d8 Mon Sep 17 00:00:00 2001 From: sthompsonCH <105206260+sthompsonCH@users.noreply.github.com> Date: Wed, 22 Jan 2025 11:26:05 +0000 Subject: [PATCH] DSND-3213: Handle 404 from exemptions api (#167) * Behaviour has been changed to return an empty optional when exemptions GET returns 404. Rather than throw an exception. It's possible to have PSCs without exemptions. --- .../service/CompanyExemptionsApiService.java | 10 +++------- .../service/CompanyExemptionsApiServiceTest.java | 6 +++--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiService.java b/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiService.java index 64f87927..c8d812c8 100644 --- a/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiService.java +++ b/src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiService.java @@ -15,7 +15,6 @@ import uk.gov.companieshouse.logging.Logger; import uk.gov.companieshouse.logging.LoggerFactory; import uk.gov.companieshouse.pscdataapi.exceptions.BadGatewayException; -import uk.gov.companieshouse.pscdataapi.exceptions.ResourceNotFoundException; import uk.gov.companieshouse.pscdataapi.logging.DataMapHolder; @Component @@ -31,7 +30,7 @@ public CompanyExemptionsApiService( } public Optional getCompanyExemptions(final String companyNumber) { - ApiResponse response; + ApiResponse response = null; try { response = exemptionsApiClientSupplier.get() .privateDeltaResourceHandler() @@ -39,12 +38,9 @@ public Optional getCompanyExemptions(final String companyNumb .execute(); } catch (ApiErrorResponseException ex) { final int statusCode = ex.getStatusCode(); - LOGGER.info("Company Exemptions API call failed with status code [%s]".formatted(statusCode), + LOGGER.info("Company Exemptions API GET failed with status code [%s]".formatted(statusCode), DataMapHolder.getLogMap()); - if (statusCode == 404) { - throw new ResourceNotFoundException(HttpStatus.NOT_FOUND, - "Company Exemptions API responded with 404 Not Found"); - } else { + if (statusCode != 404) { throw new BadGatewayException("Error calling Company Exemptions API endpoint", ex); } } catch (URIValidationException ex) { diff --git a/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiServiceTest.java b/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiServiceTest.java index a5ace2c5..523cfa7c 100644 --- a/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiServiceTest.java +++ b/src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiServiceTest.java @@ -89,7 +89,7 @@ void shouldGetCompanyExemptionsWithNullResponseAndReturnEmptyOptional() throws E } @Test - void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws Exception { + void shouldReturnEmptyOptionalWhenApiRespondsWith404NotFound() throws Exception { // given when(supplier.get()).thenReturn(client); when(client.privateDeltaResourceHandler()).thenReturn(privateDeltaResourceHandler); @@ -98,11 +98,11 @@ void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws when(privateCompanyExemptionsGetAll.execute()).thenThrow(buildApiErrorResponseException(404)); // when - Executable executable = () -> service.getCompanyExemptions(COMPANY_NUMBER); + final Optional actual = service.getCompanyExemptions(COMPANY_NUMBER); // then - assertThrows(ResourceNotFoundException.class, executable); verify(privateDeltaResourceHandler).getCompanyExemptionsResource(URL); + assertEquals(Optional.empty(), actual); } @ParameterizedTest