Skip to content

Commit 81c7601

Browse files
authored
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.
1 parent 9e1f27f commit 81c7601

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/main/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiService.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import uk.gov.companieshouse.logging.Logger;
1616
import uk.gov.companieshouse.logging.LoggerFactory;
1717
import uk.gov.companieshouse.pscdataapi.exceptions.BadGatewayException;
18-
import uk.gov.companieshouse.pscdataapi.exceptions.ResourceNotFoundException;
1918
import uk.gov.companieshouse.pscdataapi.logging.DataMapHolder;
2019

2120
@Component
@@ -31,20 +30,17 @@ public CompanyExemptionsApiService(
3130
}
3231

3332
public Optional<CompanyExemptions> getCompanyExemptions(final String companyNumber) {
34-
ApiResponse<CompanyExemptions> response;
33+
ApiResponse<CompanyExemptions> response = null;
3534
try {
3635
response = exemptionsApiClientSupplier.get()
3736
.privateDeltaResourceHandler()
3837
.getCompanyExemptionsResource("/company/%s/exemptions".formatted(companyNumber))
3938
.execute();
4039
} catch (ApiErrorResponseException ex) {
4140
final int statusCode = ex.getStatusCode();
42-
LOGGER.info("Company Exemptions API call failed with status code [%s]".formatted(statusCode),
41+
LOGGER.info("Company Exemptions API GET failed with status code [%s]".formatted(statusCode),
4342
DataMapHolder.getLogMap());
44-
if (statusCode == 404) {
45-
throw new ResourceNotFoundException(HttpStatus.NOT_FOUND,
46-
"Company Exemptions API responded with 404 Not Found");
47-
} else {
43+
if (statusCode != 404) {
4844
throw new BadGatewayException("Error calling Company Exemptions API endpoint", ex);
4945
}
5046
} catch (URIValidationException ex) {

src/test/java/uk/gov/companieshouse/pscdataapi/service/CompanyExemptionsApiServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void shouldGetCompanyExemptionsWithNullResponseAndReturnEmptyOptional() throws E
8989
}
9090

9191
@Test
92-
void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws Exception {
92+
void shouldReturnEmptyOptionalWhenApiRespondsWith404NotFound() throws Exception {
9393
// given
9494
when(supplier.get()).thenReturn(client);
9595
when(client.privateDeltaResourceHandler()).thenReturn(privateDeltaResourceHandler);
@@ -98,11 +98,11 @@ void shouldThrowResourceNotFoundExceptionWhenApiRespondsWith404NotFound() throws
9898
when(privateCompanyExemptionsGetAll.execute()).thenThrow(buildApiErrorResponseException(404));
9999

100100
// when
101-
Executable executable = () -> service.getCompanyExemptions(COMPANY_NUMBER);
101+
final Optional<CompanyExemptions> actual = service.getCompanyExemptions(COMPANY_NUMBER);
102102

103103
// then
104-
assertThrows(ResourceNotFoundException.class, executable);
105104
verify(privateDeltaResourceHandler).getCompanyExemptionsResource(URL);
105+
assertEquals(Optional.empty(), actual);
106106
}
107107

108108
@ParameterizedTest

0 commit comments

Comments
 (0)